What is the reason for multiple resolution / rejection in ES6 Promise

I found that it is possible (in ES6 promises while the Promise object is created) to use a multiple decision / rejection that will affect PromiseStatus only once but not affect the execution flow.

var p = new Promise(function(resolve, reject) { 
    setTimeout(function(){
        resolve(1);
        console.log('Resolve 1');
    }, 50);
    setTimeout(function(){
        resolve(2);
        console.log('Resolve 2');
    }, 100);
});

setTimeout(function(){
        console.log('Status #1:', p);
    }, 10);
setTimeout(function(){
        console.log('Status #2:', p);
    }, 60);
setTimeout(function(){
        console.log('Status #3:', p);
    }, 110);

p.then(function(x){
    console.log('Value after:', x)
})

In then()the first resolve / reject function, they will affect the flow of execution. So my question is: why does it work like this (function / error)?

PS My env Node 4.1

PPS My conclusion:

Status #1: Promise { <pending> }
Resolve 1
Value after: 1
Status #2: Promise { 1 }
Resolve 2
Status #3: Promise { 1 }
+4
source share
2 answers

, , . Promises - , . 5, , 3.

, resolve, . , - ?

throw - - , throw . .catch , .

, , , ( ). ( ). - .

+7

ECMAScript 2015, Promise Reject Functions Promise Resolve Functions ,

  1. . [[value]] true, undefined.

, , , Promise. , /.

+8

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


All Articles