Skip to content

Instantly share code, notes, and snippets.

@iamdtang
Last active April 10, 2018 04:14
Show Gist options
  • Save iamdtang/46f0f399a02b99fda1c54f6c4efe1323 to your computer and use it in GitHub Desktop.
Save iamdtang/46f0f399a02b99fda1c54f6c4efe1323 to your computer and use it in GitHub Desktop.
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