Skip to content

Instantly share code, notes, and snippets.

@ryanburnette
Created August 20, 2020 16:33
Show Gist options
  • Save ryanburnette/05a937ca7bb1c019c5b919adb107f03a to your computer and use it in GitHub Desktop.
Save ryanburnette/05a937ca7bb1c019c5b919adb107f03a to your computer and use it in GitHub Desktop.
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
Copy link

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