javascript - Combine data of two async requests to answer both requests -


i'm trying bring 2 async data sources (each of them arrive @ server via post request , have common, otherwise unique id). answer of 2 post requests, need data supplied both post requests. data structure, library etc. can use let each of post handlers wait others value?

edit:

to illustrate problem code: have 2 functions, let's call them back , front, called @ arbitrary times:

var synchronizer = //what i'm looking  function front(req) {   var id = req.data.id   var frontvalue = req.data.value;   synchronizer.supplyfrontvalue(id, frontvalue).then(function(backvalue){     req.send("the product " + (frontvalue*backvalue));   });   // or alternatively, instead of promise, use callback:   // synchronizer.supplyfrontvalue(id, frontvalue, function(backvalue){   //   req.send("the product " + (frontvalue*backvalue));   // }); }  function back(req) {   var id = req.data.id   var backvalue = req.data.value;   synchronizer.supplybackvalue(id, backvalue).then(function(frontvalue){     req.send("the quotient " + (frontvalue/backvalue));   }); } 

you use promises that. native promises supported in every newer browsers (see http://caniuse.com/#feat=promises), others use fallback library.

example:

var request1 = new promise(function(resolve) {     doajaxrequest("someurl", function(payload) {         resolve(payload);     }); });  var request2 = new promise(function(resolve) {     doajaxrequest("someotherurl", function(payload) {         resolve(payload);     }); });  promise.all([request1, request2]).then(function(payloads) {     console.log("all payload", payloads); }); 

Comments