Skip to content

Instantly share code, notes, and snippets.

@johndaley-me
Last active July 23, 2017 02:20
Show Gist options
  • Save johndaley-me/8b4ef95dcb71938f723630c5e08509f5 to your computer and use it in GitHub Desktop.
Save johndaley-me/8b4ef95dcb71938f723630c5e08509f5 to your computer and use it in GitHub Desktop.
Overusing await
function findFooById(id) {
// pretend this is a DB query or something asynchronous
return Promise.resolve({
id,
name: 'Foo'
});
}
async function findFoo1() {
console.log('finding foo 1');
const rval = await findFooById(1);
console.log('I have foo 1');
return rval;
}
function findFoo2() {
console.log('finding foo 2');
return findFooById(2);
}
export default async function () {
const foo1P = findFoo1();
const foo1 = await foo1P;
console.log('I finally have foo 1');
const foo2P = findFoo2();
const foo2 = await foo2P;
console.log('I finally have foo 2');
return [await foo1, await foo2];
}
// Console output:
// finding foo 1
// I have foo 1
// I finally have foo 1
// finding foo 2
// I finally have foo 2
// [{"id":1,"name":"Foo"},{"id":2,"name":"Foo"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment