Skip to content

Instantly share code, notes, and snippets.

@oahehc
Last active July 26, 2018 05:51
Show Gist options
  • Save oahehc/1c88f21b2f03d9e25c823ed6dc4767c5 to your computer and use it in GitHub Desktop.
Save oahehc/1c88f21b2f03d9e25c823ed6dc4767c5 to your computer and use it in GitHub Desktop.
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