Node.js 节点异步系列块中的执行不会等待回调触发

Node.js 节点异步系列块中的执行不会等待回调触发,node.js,asynchronous,Node.js,Asynchronous,我需要同步实现RESTAPI,以便它返回一个对象列表。因此,顶级方法的实现如下所示,其中方法listObjs()应同步执行,并返回可用的对象,以便在REST响应中返回它们 var objs= mds.listObjs(); console.log("Leaving the GET /objs API!! ["+objs+"]"); res.json({ message :'"Leaving the GET /objs API!! ['+objs+']'}) 在方法listObjs()中,

我需要同步实现RESTAPI,以便它返回一个对象列表。因此,顶级方法的实现如下所示,其中方法listObjs()应同步执行,并返回可用的对象,以便在REST响应中返回它们

 var objs= mds.listObjs();
 console.log("Leaving the GET /objs API!! ["+objs+"]");
 res.json({ message :'"Leaving the GET /objs API!! ['+objs+']'})
在方法listObjs()中,我使用async.series将处理分为几个阶段,一个接一个地执行。第一阶段将获取对象,第二阶段将处理它们并向调用者返回子集。getHttpRequest方法在内部使用回调来捕获请求的结果。可以看到,我使用这个回调来执行异步回调。这将通知async.series进入下一阶段

  listObjs : function() {
      async.series([
           function (callback) {
               console.log("Before getHttp");
               getHttpRequest(httpQuery,
                       function(responseStatusCode, objectsJson) {
                               console.log("Now Trigger the next async step")
                               callback(null, objectsJson); // trigger next step
                        });
               console.log("After getHttp");
            },
            function (callback) {
                console.log("Next Step, process the returned Objects");
                callback(null, "2");
            }
        ],
        function (err, result) {
             console.log("At end of async block "+result);
        });
        console.log("Leaving listObjs, SHOULD BE AFTER THE NEXT STEP IS CALLED");
    },
我观察到的是,async.series块不会等到它的回调被调用。相反,执行线程似乎跳出了异步块,顶级方法在其结果可用之前返回:-

  • getHttp之前
  • 在getHttp之后
  • 离开listObjs应该在调用下一步之后
  • 离开GET/objsapi!![]
  • 现在触发下一个异步步骤
  • 下一步,处理返回的对象,返回结果=
  • 在异步块[{“name”:“obj1”},{“name”:“obj2”}的末尾,2
  • 那么,为什么在执行第二阶段之前退出async.series块呢? 也就是说,为什么是信息4。不是在我的跟踪消息末尾


    提前非常感谢

    输出正确!它们是异步的,它们将在自己的任务完成时执行。这是您的完整代码

    var data = {};
    var getData = function (callback) {
    
      // this is asyn task, it will take time to complete
      getHttpRequest(httpQuery, function(responseStatusCode, objectsJson) {
    
        // save your data
        data = objectsJson;
        callback(null, objectsJson); // trigger next step, objectsJson will be avaible at the last callback
      });
    
      // meanwhile this line will be executed
      console.log("I will be executed");
    }
    
    
    var processData = function (callback) {
      // do your job here, with data varibale
    
      // callback(err, result)
      // if you envoke callback with error then next function of the async.series will not be executed
      if(someThingGoWrong)
        return callback(new Error("Your error")); //  TagOne
    
    }
    
    var anotherTask = function(callback){
      if(someThingGoWrong)
        return callback("You can also send error as a string"); // TagTwo
    
      // another tasks
      data =  updatedValues
    }
    
    var finalHandler = function (err, result) {
      // if any error happen in TagOne or TagTwo
      // this final handler will be immediate executed
    
      // so handle error here
    
      if(err) return res.status(400).json(message:"Something bad happend");
    
      res.status(200).json({message: 'ok', data:data});
    }
    
    // execute the series
    async.series([getData, processData, anotherTask], finalHandler);
    

    您的列表很难与代码关联,您能将列表中的每个数字注释到代码段中的相应区域吗?