Last active
April 10, 2018 04:14
-
-
Save iamdtang/46f0f399a02b99fda1c54f6c4efe1323 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function myAsyncFunction() { | |
doSomethingAsync1(function(error, data1) { | |
doSomethingAsync2(data1, function(error, data2) { | |
console.log(data2); | |
}); | |
}); | |
} |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
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