Skip to content

Instantly share code, notes, and snippets.

@foowaa
Created July 31, 2019 18:24
Show Gist options
  • Save foowaa/68d060039b8119d1b968b84e9354c29f to your computer and use it in GitHub Desktop.
Save foowaa/68d060039b8119d1b968b84e9354c29f to your computer and use it in GitHub Desktop.
//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