N
N
nickerlan2019-07-09 20:26:27
JavaScript
nickerlan, 2019-07-09 20:26:27

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)
}

I want data to be sent in response to a request, and after sending it, an asynchronous call could be completed.
In this regard, the question is - if I just leave the call without await - will it pass? Will the program end before the request is made? Will the presence/absence of .then() affect anything?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Robur, 2019-07-09
@Robur

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.

D
Daniil Bratukhin, 2019-07-10
@dantothefuture

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)
}

N
nickerlan, 2019-07-11
@nickerlan

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}

If there is a better solution, please let me know.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question