Skip to content

Instantly share code, notes, and snippets.

@infantito
Last active February 25, 2020 23:35
Show Gist options
  • Save infantito/3a712a6329123eaf842a5a0fd4078b72 to your computer and use it in GitHub Desktop.
Save infantito/3a712a6329123eaf842a5a0fd4078b72 to your computer and use it in GitHub Desktop.
TIP: When making two asynchronous calls, ask yourself if you want to make those calls in serial or parallel
// code: https://twitter.com/joelnet/status/1231994830798061568
const sum = (a, b) => Promise.resolve(a + b);
const subtract = (a, b) => Promise.resolve(a - b);
// Promises
const promises = async () => {
const p1 = sum(7, 3);
const p2 = sum(8, 5);
return Promise.all([p1, p2]);
}
// Async - await
const asyncs = async () => {
const p1 = await sum(8, 5);
const p2 = await sum(7, 3);
// if you check out the thread, then:
// return await Promise.all([p1, p2]);
return [await p1, await p2];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment