简体   繁体   English

如何确保节点js在进行异步调用后返回数据?

[英]How to make sure that node js returns data back after making asynchronous calls?

I was asked this question in an interview today that say node.js is used to make 3 REST webservice calls. 今天在一次采访中有人问我这个问题,说node.js用于进行3个REST Web服务调用。 The calls are independent and so you can make them in parallel. 调用是独立的,因此您可以并行进行。 However towards the end of the method, you need to wait for all the webservice calls to return result. 但是,在方法结束时,您需要等待所有Web服务调用返回结果。 You need to collate all the results and send it back. 您需要整理所有结果并将其发送回去。

I said that I will chain the promises and return the result only from the resolve of the third promise. 我说过,我将束缚诺言,仅从第三个诺言的决心中返回结果。 However, the interviewer was not satisfied with the answer. 但是,面试官对答案并不满意。 I am not sure if there is anything I missed. 我不确定是否有什么我想念的。 Please let me know. 请告诉我。

EDIT : I have added the below notes to explain my solution. 编辑:我添加了以下说明来解释我的解决方案。

My solution was - 我的解决方案是-

var p1 = new Promise() // From here to make the first call
var p2 = new Promise() // Second Call
var p3 = new Promise() //Third Call

p1.resolve(p2.resolve()).p3.resolve( return result)

I thought that this way we would block the call until the data from all the three services are retrieved. 我以为这样,我们将阻止该调用,直到检索到所有三个服务的数据为止。

I use the Promise.all() method to verify that all promises were succesfully resolved. 我使用Promise.all()方法来验证所有诺言都已成功解决。

The Promise.all() method returns a single Promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first promise that rejects. Promise.all()方法返回一个Promise,当可迭代参数中的所有promise已解决或因第一个promise拒绝的原因而拒绝时,该Promise会解决。

MDN Reference, For More Info MDN参考,有关更多信息

Example: 例:

The webServicePromises in the example below would be an array of promises. 以下示例中的webServicePromises将是一个promise数组。

  Promise.all(webServicePromises).then((responses) => {
        for(let i = 0; i< responses.length; i++){
           let response = responses[i];
           //do stuff with each web service response
        }
        },  reason => {
            console.log(reason);
        }).catch(error => logError(`${error}`));

Try using this package. 尝试使用软件包。

async.parallel({
    one: function(callback) {
        setTimeout(function() {
            callback(null, 1);
        }, 200);
    },
    two: function(callback) {
        setTimeout(function() {
            callback(null, 2);
        }, 100);
    },
    three: function(callback) {
        setTimeout(function() {
            callback(null, 3);
        }, 50);
    }
}, function(err, results) {
    // results is now equals to: {one: 1, two: 2, three: 3}
});

Above example is aync package's parallel api, it has many more. 上面的示例是aync软件包的并行api,它还有更多功能。 If you are working on node then this library is quite handly. 如果您在节点上工作,那么此库非常方便。

Let me know in case any doubt. 如有任何疑问,请通知我。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM