Created
September 22, 2019 18:59
-
-
Save seanpierce/7992160c781b28ee408b6c4602bc4cfb to your computer and use it in GitHub Desktop.
An example about how to implement async, promise-based JS functionality.
This file contains 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 logOne() { | |
return new Promise((res) => { | |
setTimeout(() => { | |
console.log('one'); | |
res(); | |
}, Math.random() * 2000) | |
}) | |
} | |
function logTwo() { | |
setTimeout(() => { | |
console.log('two'); | |
}, Math.random() * 1000) | |
} | |
function inOrder(one, two) { | |
one().then(() => { | |
two(); | |
}) | |
} | |
inOrder(logOne, logTwo); | |
// one | |
// two | |
// it should always log those two in order regardless of their timing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment