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