Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
playing around with async/await... it's not as useful as I had hoped it would be
var _foo = 0;
async function foo() {
return new Promise(function (resolve) {
setTimeout(function () {
_foo++;
console.log(_foo);
resolve(_foo);
}, 1000);
});
}
async function useAwait() {
await foo();
await foo();
await foo();
await foo();
await foo();
}
async function usePromiseChain() {
foo()
.then(function () {
return foo();
})
.then(function () {
return foo();
})
.then(function () {
return foo();
})
.then(function () {
return foo();
});
}
async function needsPromiseAll() {
Promise.all([foo(), foo(), foo()])
.then(function () {
return foo();
})
.then(function () {
return foo();
});
}
// useAwait();
// usePromiseChain();
needsPromiseAll();
@coolaj86

This comment has been minimized.

Copy link

@coolaj86 coolaj86 commented Aug 20, 2020

You forgot one:

async function needsPromiseAll() {
  return await Promise.all([foo(), foo(), foo()]).then(foo).then(foo);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.