Created
August 21, 2019 00:20
-
-
Save joewright/0fb5d63b12eff5c78b0f29f0b1ba2506 to your computer and use it in GitHub Desktop.
Async/await example
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
asyncAwaitTest(); | |
async function asyncAwaitTest() { | |
let result1, result2, result3, results; | |
try { | |
result1 = promise1(); | |
result2 = promise2(); | |
result3 = promise3(); | |
results = [await result1, await result2, await result3]; | |
} catch(err) { | |
console.error('dang'); | |
} | |
console.log(results); | |
} | |
function promise1() { | |
return new Promise(resolve => { | |
console.log('promise1 start'); | |
setTimeout(() => { | |
console.log('promise1 done'); | |
resolve('promise1'); | |
}, 100); | |
}); | |
} | |
function promise2() { | |
return new Promise(resolve => { | |
console.log('promise2 start'); | |
setTimeout(() => { | |
console.log('promise2 done'); | |
resolve('promise2'); | |
}, 300); | |
}); | |
} | |
function promise3() { | |
return new Promise(resolve => { | |
console.log('promise3 start'); | |
setTimeout(() => { | |
console.log('promise3 done'); | |
resolve('promise3'); | |
}, 200); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment