Is prom.all useful given that javascript is executed in a single thread?

In, for example, kriskowal Q, you can do something like:

promise1.then(function(p1){
  var p2 = makePromise2();
  var p3 = makePromise3();
  var p4 = makePromise4();
  return [p2, p3, p4];
})
.all(promises, function(){
  console.log('all promises fulfilled');
}, function(reason){
  console.log('a promise was rejected: ' + reason.toString());
});

Given that javascript is executed in a single thread, does this have any advantage, performance, or something else just by making a series of then () calls?

+4
source share
2 answers

First of all, JavaScript as a language does not say anything about concurrency. In fact, in many scenarios you can create and run multi-threaded JavaScript-WebWorkers on the Internet, in node, and independently integrate the JS engine into an existing application.

, JavaScript ( ES2015). - , JavaScript , API.

DOM JavaScript , - , JavaScript. concurrency IOCP ( /) Windows.

, , AJAX. , URL-, XHR.

// with all
var a = ["url1","url2","url3"].map(makeAjaxPromise); // make 3 ajax calls
Promise.all(a).spread(function(res1,res2,res3){ // Q.all with Q
     alert("Everything loaded!");
});

ajax . , JavaScript - , , , , , promises.

,

 makeAjaxPromise("url1").
 then(makeAjaxPromise.bind(null,"url2").
 then(makeAjaxPromise.bind(null,"url3").then(function(){
      alert("Everything loaded!"); // disregarding getting the data here
 });

, , , , .

, :

  • JavaScript- 3 API DOM
  • DOM API XHR, JavaScript JavaScript
  • , JavaScript , .

  • JavaScript DOM API.
  • DOM API , XHR, JavaScript.
  • , JavaScript : -JavaScript- DOM API.
  • DOM API , XHR, JavaScript.
  • , JavaScript : -JavaScript- DOM API.
  • DOM API , XHR, JavaScript.
  • , JavaScript :
  • .

, , JavaScript , , .

+14

:

JavaScript .

, iframe, , API- Windows , .

API DOM/HTML5 API-, W3C , -, .

node.js JavaScript , node . API.

JS-, Wakanda, RingoJS, TeaJS, APE, SilkJS , JavaScript API- (Wakanda API API- , )

+3

Source: https://habr.com/ru/post/1534884/


All Articles