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