Created
July 31, 2019 18:24
-
-
Save foowaa/68d060039b8119d1b968b84e9354c29f to your computer and use it in GitHub Desktop.
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
//asynchronous | |
function logWord(word) { | |
setTimeout( | |
function() { | |
console.log(word); | |
}, | |
Math.floor(Math.random() * 100) + 1 | |
// return value between 1 ~ 100 | |
); | |
} | |
function logAll() { | |
logWord("A"); | |
logWord("B"); | |
logWord("C"); | |
} | |
logAll(); | |
Callbacks; | |
function logWord(word, callback) { | |
setTimeout(function() { | |
console.log(word); | |
callback(); | |
}), | |
Math.floor(Math.random() * 100) + 1; | |
} | |
function logAll() { | |
logWord("A", function() { | |
logWord("B", function() { | |
logWord("C", function() {}); | |
}); | |
}); | |
} | |
logAll(); | |
// Promises | |
function logWord(word) { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
console.log(word); | |
resolve(); | |
}, Math.floor(Math.random() * 100) + 1); | |
}); | |
} | |
function logAll() { | |
logWord("A") | |
.then(function() { | |
return logWord("B"); | |
}) | |
.then(function() { | |
return logWord("C"); | |
}); | |
} | |
logAll(); | |
//ansyc-await syntactic sugar | |
async function logAll() { | |
await logWord("A"); | |
await logWord("B"); | |
await logWord("C"); | |
} | |
logAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment