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

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

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

1a18bf1e50d7d2bdfe52a6c9fceec244?s=128

saiya_moebius

December 04, 2020
Tweet

Transcript

  1. None
  2. None
  3. None
  4. Promise Deferred Objects async await Promise async await

  5. None
  6. Promise<T> ( ) ( ) resolve(T) Promise reject(any) Promise then(f,

    f) , ,
  7. Promise<T> ( ) ( ) resolve(T) Promise reject(any) Promise then(f,

    f) , ,
  8. async return throw await const value = await ; console.debug(value);

    .then((value) => { console.debug(value); });
  9. None
  10. None
  11. None
  12. const promise = new Promise(); // ... ... promise.resolve( );

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

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

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

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

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

    => { // reject( ) }); } const promise = (); // ↑ Promise reject ※1
  19. promise.then .then((result) => { return (); // Promise }).then((result) =>

    { // result === });
  20. Promise.all(Promise<T>[]): Promise<T[]> Promise.race(Promise<T>[]): Promise<T> promise.catch(f)

  21. function myFunction() { A().then((resultA) => { if (resultA === "hoge")

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

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

    "hoge") { if (await B() === "huga") C(); } else { if (await D() === "piyo") E(); } } await then
  26. None
  27. Promise Promise.all Promise.race await Promise.all( 1(), 2(), ...) Promise

  28. async await async Promise Promise await await

  29. None
  30. None
  31. async await function hoge() { const async = 1; const

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

    2(); // await 2(); await 3(); // 3().then(...) } 2 no-floating-promises
  33. await Promise.resolve(Promise.resolve(123)); // Promise.resolve(123) 123 // Promise.resolve Promise

  34. fulfill reject resolve Promise.reject(e) Promise.resolve(value) fulfill resolve reject

  35. new Promise Promise. Promise

  36. Promise Promise Promise = undefined; // Promise.resolve(1234); // await (async()

    => 123)(); // async await Promise Promise ZoneAwarePromise Promise Zone async await Promise.
  37. await then await 14.6.13 Runtime Semantics: Evaluation 25.5.5.3 AsyncFunctionAwait (

    value ) 25.4.5.3.1 PerformPromiseThen
  38. await