Reputation: 6512
My local tests find that the question is true.
Given the following code, where 'asyncOne' and 'asyncTwo' are asynchronous functions, if 'asyncTwo' completes before 'asyncOne' does 'asyncOne' still get time to finish execution?
router.get("/", function (req, res, next) {
asyncOne(function () {
console.log("Completed asyncOne");
});
asyncTwo(function () {
console.log("Completed asyncTwo");
return res.json("Done.");
});
});
Upvotes: 0
Views: 61
Reputation: 378
Maybe you shoud call your functions like this:
router.get("/", function (req, res, next) {
Promise.all[asyncOne(), asyncTwo()].then(() => {
return res.json("Done.");
});
});
or like this:
router.get("/", function (req, res, next) {
asyncOne(function () {
console.log("Completed asyncOne");
asyncTwo(function () {
console.log("Completed asyncTwo");
return res.json("Done.");
});
});
});
Upvotes: 1
Reputation: 888
Yes all code will execute. But you cannot return res.json("Done.");
in the asyncOne
function, because a end response has already been sent.
If you would like a return to be called when both async functions are done you should checkout the async module. It's really great.
Upvotes: 1