Skip to content

Instantly share code, notes, and snippets.

@safebear
Created March 8, 2019 11:06
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 safebear/2a762a377e4a784dbdbe10d83fed7e5b to your computer and use it in GitHub Desktop.
Save safebear/2a762a377e4a784dbdbe10d83fed7e5b to your computer and use it in GitHub Desktop.
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