Skip to content

Instantly share code, notes, and snippets.

@skaterdav85 skaterdav85/1-callbacks.js
Last active Apr 10, 2018

Embed
What would you like to do?
function myAsyncFunction() {
doSomethingAsync1(function(error, data1) {
doSomethingAsync2(data1, function(error, data2) {
console.log(data2);
});
});
}
function myAsyncFunction() {
doSomethingAsync1()
.then(function(data1) {
return doSomethingAsync2(data1);
}, function() {
// handle error
})
.then(function(data2) {
console.log(data2);
});
}
// or
function myAsyncFunction() {
doSomethingAsync1()
.then(doSomethingAsync2, handleError);
.then(function(data2) {
console.log(data2);
});
}
function handleError() {
// handle error
}
async function myAsyncFunction() {
try {
let data1 = await doSomethingAsync1();
let data2 = await doSomethingAsync2(data1);
} catch(error) {
// handle error
}
}
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.