Answer the question
In order to leave comments, you need to log in
Do promises burn out after res.end?
I have mirkoservis.
I want to make part of the logic asynchronous:
async (req,res)=>{
var data = await action1(data); //синхронный вызов
action2(data) // асинхронный вызов
//.then() // нужен ли then?
res.end(data)
}
Answer the question
In order to leave comments, you need to log in
Promises are not bonuses, they do not expire.
They will either ever resolve or they will never.
I don't know what "pass" means. The function will be called, and what happens depends on that function.
If something works for you, it won't. request processing is just a function call, same as doing something in setTimeout()
Will affect how it all works. and what will be the result depends on what you write.
To you about all it to take and esteem normal to dock and to understand.
Depends on your microservice manager implementation. Take, for example, Google Cloud Functions - the service waits for a successful Promise or an error, and after that it can do anything. It can leave your particular instance of Node running (and it will most likely), or it can end the process right away. So it is more correct in this case to first respond to the client, and then wait for the completion of asynchronous operations:
async (req, res) => {
const data = await action1(data)
res.end(data)
await action2(data)
}
Found out: after sending the request - the lambda is completed.
At least in the case of Zeit Now, which is an add-on for AWS Lambdas.
When I asked what to do about it, they replied "You are looking for queue support. You would put that job in queue to be consumed by another resource. We will release this in the future and I will contact you when its live."
It seems that you will have to use a crutch - wrap the entire asynchronous part, because this is in the example, there is only one function. In fact, there are many of them, and they sit in different modules.
const __async=(promise)=>{
if (!process.promises || !Array.isArray(process.promises)) process.promises=[];
process.promises.push(promise)
}
const __withasync= (handler)=>{
return async (req,res)=>{
var result = await handler(req,res);
if (process.promises && Array.isArray(process.promises)) await Promise.all(process.promises);
return result;
}
}
module.exports={__async,__withasync}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question