Skip to content

Instantly share code, notes, and snippets.

@seanpierce
Created September 22, 2019 18:59
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 seanpierce/7992160c781b28ee408b6c4602bc4cfb to your computer and use it in GitHub Desktop.
Save seanpierce/7992160c781b28ee408b6c4602bc4cfb to your computer and use it in GitHub Desktop.
An example about how to implement async, promise-based JS functionality.
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