如何同步2个或更多asynchronous请求的数据,并发送parsing​​数据的响应?

通过最简单的例子就是说,他们是2个或更多的需要通过发出HTTP请求提交数据的帅哥,由其中一个帅哥提交的数据必须等待其他数据由其他帅哥发送到服务器为了parsing。 这里是我想要在Node.js中完成的一个图像:

在这里输入图像说明

当数据被parsing时,需要将其作为回应发送给每个男人。 问题是,我坚持实施这个任务(我使用http模块和长时间轮询,后来我会使用websocket),问题驻留在callback性质,我尝试使用脏方法,如做一个数组是直到所有人都提交,并在setInterval函数的帮助下检查,无论何时何地parsing数据等等。

有没有更清洁,更杂乱的方式来执行这样的任务?

我会给这个过程(这两个客户端都知道)提供一个唯一的ID,然后将相关数据存储在数据库中或直接存储在服务器RAM中。 有待处理的请求(请求 – 等待)对于服务器来说是不利的,因为它必须保持越来越多的开放连接,并且对于客户端来说是不利的,因为他不知道发生了什么。 我宁愿做以下事情:

1. client sends request with data 2. data gets stored, a *please wait* is returned 3. the page reloads all two seconds 4. if all the datas there, return the data 5. if not, return *please wait* and goto 3 

伪实现将如下所示:

 var processes = new Map(); function Process (){ do { var id = Math floor(Math.random()*10**10); }while(processes.has(id)); this.id = id; processes.set(id,this); } Process.prototype.pending = function(res){ if(this.first && this.second) return res.end(this.first + this.second); res.end(` <html> <body> Please wait... <script> setTimeout(location.reload.bind(location),2000); </script> </body> </html>`); }; //the routing var app = Express(); app.get("/create",function(req,res){ var p = new Process(); req.end(p.id); }); app.get("/first/:id/:data",function(req,res){ var p = processes.get(req.params.id); if(!p) return res.end("id not found"); p.first = req.params.data; p.pending(res); }); app.get("/second/:id/:data",function(req,res){ var p = processes.get(req.params.id); if(!p) return res.end("id not found"); p.second = req.params.data; p.pending(res); }); 

目前最简单的方法是使用Promise.all

 function firstAsyncRequest() { return new Promise((resolve, reject) => { // do your async call and then resolve(); // or reject() if any error; }); } function secondAsyncRequest() { return new Promise((resolve, reject) => { // do your async call and then resolve(); // or reject() if any error; }); } Promise.all([firstAsyncRequest, secondAsyncRequest]) .then(arrayOfResults => { //do your logic }) .catch(err => { //catch your error }) 

如果使用callback,最简单的方法是使用async模块,然后使用async.parallel

 async.parallel([ function(callback) { setTimeout(function() { callback(null, 'one'); }, 200); }, function(callback) { setTimeout(function() { callback(null, 'two'); }, 100); } ], // optional callback function(err, results) { // the results array will equal ['one','two'] even though // the second function had a shorter timeout. }); 

正如我在我的评论中提到的, promise.all()很好,但是一旦其中一个被拒绝,你没有任何控制来解决promise。

相反,您可以使用.reduce()来排列您的承诺,并且在解决和拒绝的承诺上您将拥有更多的控制权。

 var pr1 = new Promise((v,x) => setTimeout(_ => Math.random() > 0.2 ? v("promise 1 resolved value") : x("Promise 1 rejected"),150)), pr2 = new Promise((v,x) => setTimeout(_ => Math.random() > 0.2 ? v("promise 2 resolved value") : x("Promise 2 rejected"),120)), pr3 = new Promise((v,x) => setTimeout(_ => Math.random() > 0.2 ? v("promise 3 resolved value") : x("Promise 3 rejected"),60)), pr4 = new Promise((v,x) => setTimeout(_ => Math.random() > 0.2 ? v("promise 4 resolved value") : x("Promise 4 rejected"),220)), pr5 = new Promise((v,x) => setTimeout(_ => Math.random() > 0.2 ? v("promise 5 resolved value") : x("Promise 5 rejected"),180)); [pr1,pr2,pr3,pr4,pr5].reduce((p,c) => p.then(v => (console.log(`doing something with ${v} and will proceed with the next`),c), x => (console.log(`Error ${x} however i will proceed with the next`),c))) .then(v => console.log(`finally doing something with ${v}`), x => console.log(`Error ${x} received`));