Reputation: 6360
Title is long question but if I make an asynchronous call in javascript but do not have a call back or chain it in a promise will it complete execution? i.e. If I have code like:
function markAsInactive(userId) {
return retrieveUser(userId) //
.then(res => {
const mark = markAsInactive(userId) // returns a promise (say takes 2 seconds)
return Promise.resolve('blah')
})
.then(...etc)
.catch(...)
}
The call to markAsInactive
is fired but the thread does not wait for it to resolve and instead will move on to the next then
immediately (is my understanding). Would it be guaranteed to complete execution or would the thread/process cancel the call if the stack returns back to the root?
I know that I can use something like return Promise.all([mark, ...])
to have multiple promises resolve in parallel; this question is more for my edification.
Upvotes: 1
Views: 57
Reputation: 1075239
Would it be guaranteed to complete execution or would the thread/process cancel the call if the stack returns back to the root?
It completes the call. All asynchronous operations end up exiting the current job (task) before further work on them is done, completely unwinding the stack; it's intrinsic to the job-based nature of JavaScript on a given thread (and NodeJS uses a single thread) and the fact that promise then
callbacks are guaranteed to be asynchronous.
The work wouldn't be completed if NodeJS terminated before it was done, but NodeJS keeps running while there are any outstanding tasks, I/O operations, etc.
Upvotes: 2