Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Callbacks, 'Then' and 'Async / Await'.
function first () {
console.log(1)
}
function second () {
console.log(2)
}
first();
second();
// outputs
1
2
// add timeout to first function
function first () {
setTimeout( () => {
console.log(1)
}, 500 )
}
function second () {
console.log(2)
}
first();
second();
// outputs
2
1
// add callback to first function
function first (callback) {
setTimeout( () => {
console.log(1);
callback()
}, 500 )
}
function second () {
console.log(2)
}
first(second);
// outputs
1
2
first(second) // callbacks
first().then(second(result))
.then(third(result))
.then(forth(result))
.catch(failureCallback); // using 'then' to handle promises
async function wrapper(){ // using 'await' to handle a promise
await first();
await second();
await third();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.