Last active
July 26, 2018 05:51
-
-
Save oahehc/1c88f21b2f03d9e25c823ed6dc4767c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const a = () => ( | |
| new Promise((res) => { | |
| console.log('-- a start'); | |
| setTimeout(() => { | |
| console.log('a'); | |
| res('-- a finish'); | |
| }, 1000) | |
| }) | |
| ) | |
| const b = () => ( | |
| new Promise((res) => { | |
| console.log('-- b start'); | |
| setTimeout(() => { | |
| console.log('b'); | |
| res('-- b finish'); | |
| }, 500) | |
| }) | |
| ) | |
| // [a, b].forEach((f) => f()); | |
| const solution_a = function () { | |
| a() | |
| .then((r) => { | |
| console.log(r); | |
| return b(); | |
| }) | |
| .then((r) => { | |
| console.log(r); | |
| }); | |
| }; | |
| const solution_b = function () { | |
| [a, b].reduce((arr, f) => { | |
| return arr.then((r) => f()) | |
| }, Promise.resolve()); | |
| } | |
| async function solution_c() { | |
| var result = await a(); | |
| console.log(result); | |
| var result = await b(); | |
| console.log(result); | |
| }; | |
| solution_a(); | |
| solution_b(); | |
| solution_c(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment