Created
September 12, 2017 23:30
-
-
Save JavaTheNutt/e8a799e165ff23577cf9aa66d2d15c60 to your computer and use it in GitHub Desktop.
Testing async/await blocking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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