Upgrade to Pro — share decks privately, control downloads, hide ads and more …

async 完全理解 - 全ての async は Promise に通ず / Guide to...

async 完全理解 - 全ての async は Promise に通ず / Guide to async, await

saiya_moebius

December 04, 2020
Tweet

More Decks by saiya_moebius

Other Decks in Programming

Transcript

  1. async return throw await const value = await ; console.debug(value);

    .then((value) => { console.debug(value); });
  2. const promise = new Promise(); // ... ... promise.resolve( );

    /* */ promise.reject( ); new Promise((resolve, reject) => { // ... ... resolve( ); /* */ reject( ); });
  3. // Promise let resolve, reject; new Promise((_resolve, _reject) => {

    // resolve, reject resolve = _resolve; reject = _reject; }); resolve( ); /* */ reject( ); // ↑ ...
  4. function () { // Promise ... const promise = new

    Promise(); // ... ( promise.resolve ) ... return promise; } const promise = (); promise.resolve(null); // resolve
  5. function () { // Promise return new Promise((resolve, reject) =>

    { // ... ( resolve ) ... }); } const promise = (); // ↑ resolve
  6. function (props) { // const hoge = props.something.foobar(); // Promise

    ... const promise = new Promise(); // ... ( promise.resolve ) ... return promise; } resolve( ) reject( )
  7. function () { /* ※1 */ return new Promise((resolve, reject)

    => { // reject( ) }); } const promise = (); // ↑ Promise reject ※1
  8. function myFunction() { A().then((resultA) => { if (resultA === "hoge")

    { B().then((resultB) => { if (resultB === "huga") C(); }); } else { D().then((resultC) => { if (resultC === "piyo") E(); }); } }); }
  9. async return throw await const value = await ; console.debug(value);

    .then((value) => { console.debug(value); });
  10. await // async function myFunction() { if (await A() ===

    "hoge") { if (await B() === "huga") C(); } else { if (await D() === "piyo") E(); } } await then
  11. async await function hoge() { const async = 1; const

    await = 2; console.log(async, await); // it works } async await async function() { ... } async
  12. await async function myFunction() { await 1(); // 1().then(...) 1();

    2(); // await 2(); await 3(); // 3().then(...) } 2 no-floating-promises
  13. Promise Promise Promise = undefined; // Promise.resolve(1234); // await (async()

    => 123)(); // async await Promise Promise ZoneAwarePromise Promise Zone async await Promise.