Skip to content

Instantly share code, notes, and snippets.

@JavaTheNutt
Created September 12, 2017 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JavaTheNutt/e8a799e165ff23577cf9aa66d2d15c60 to your computer and use it in GitHub Desktop.
Save JavaTheNutt/e8a799e165ff23577cf9aa66d2d15c60 to your computer and use it in GitHub Desktop.
Testing async/await blocking
function testPromises() {
Promise.all([someAsynchronousFunction().then(() => {
console.log('first function finished');
}), someOtherAsynchronousFunction().then(() => {
console.log('second finished');
})]).then(() => {
console.timeEnd('testPromises');
})
}
async function testAsync() {
const res1 = await someAsynchronousFunction();
console.log(`res1 retrieved: ${res1}`);
res2 = await someOtherAsynchronousFunction();
console.log(`res2 retrieved: ${res2}`);
console.timeEnd('testAsync');
}
function someAsynchronousFunction() {
return new Promise((resolve, reject) => setTimeout(() => {
console.log('first called')
resolve('hello');
}, 90))
}
function someOtherAsynchronousFunction() {
return new Promise((resolve, reject) => setTimeout(() => {
console.log('second called')
resolve('hello');
}, 50))
}
//console.time('testPromises'); //average of about 102ms
//testPromises();
console.time('testAsync'); //average of about 182ms
testAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment