I have Mirkoservice.

I want to make part of logic asynchronous:

async(req, res)=>{
   var data=await action1(data);//synchronous call
   action2(data) //asynchronous call
   //.then() //do you need then?

I want the data to be sent in response to the request, and after they are sent, an asynchronous call can be completed.

In this regard, the question - if I leave just a call without await - will it pass? Will the program be completed before the request is made? Will presence/absence of .then() affect something?
  • Here you are not php.The server at the node does not end at all – Hungry5 Jul 9 '19 at 20:32
  • but in res.end(data) it will most likely go undefined without an avite, yes – Ninja9 Jul 9 '19 at 20:41
  • Ninja9, data from the first action is already there – Hungry5 Jul 9 '19 at 20:58
  • Hungry5, this is how I interpreted"if I leave just a call without await" what about the first await action1 – Ninja9 Jul 9 '19 at 21:10
  • Look.Maybe this type will help.rel="nofollow">12 – Spectral Desert Jul 10 '19 at 00:54

3 Answers 3

Promises are not bonuses, do not burn.
They will either ever kill themselves or never.
if I leave just a call without await, will it take place?
I don’t know what it means to"pass." The function will be called, and what happens depends on this function.
Will the program be completed before the request is made?

If something does not work for you, processing a request is just a function call, the same as doing something in setTimeout()
Does the presence/absence of .then() affect something?

It will affect how it will work.And what will be the result depends on what you write.

You would take about all this and read the normal dock and figure it out.
Depends on the implementation of your microservice manager.Take Google Cloud Functions, for example - the service is waiting for a successful Promise or an error, and then it can do anything.It can leave your Node instance specifically to work(it will most likely), or it can finish process right away.So it’s more correct in this case to first respond to the client, and then wait until the asynchronous operations are completed:

async(req, res)=>{
  const data=await action1(data)
  await action2(data)
  • Yes, it's lambda.
    In my case, this is Zeit Now
    In fact, I use the Micro library, and it generally turns the entire call into a function:

    module.exports=micro(async(req, res)=>{
      const data=await action1(data)
      return data
    – MTV's7 Jul 10 '19 at 11:45
  • [[nickerlan]], then you can:

    const {send}=require('micro')
    module.exports=async req=>{
      const data=await action1(data)
      send(req, 200, data)
      await action2(data)
    – Glamorous49 Jul 10 '19 at 12:32
I found out: after sending the request - the lambda ends.
At least in the case of Zeit Now, and this is an add-on for AWS Lambdas.

To my question - what to do with it, they replied,"You’re looking for some kind of support.

It looks like you have to use a crutch - the entire asynchronous part is wrapped, because this is an example, there is only one function.In fact, there are many of them, and they are sitting on different modules.

const __async=(promise)=>{
    if(! process.promises ||! Array.isArray(process.promises)) process.promises=[];
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 - let me know.