在ES6 Promise中允许多个解析/拒绝的原因是什么

Mic*_*hov 4 javascript promise ecmascript-6 es6-promise

我发现有可能(在ES6承诺中,虽然创建了Promise对象)使用多个resolve/reject,这只会影响PromiseStatus一次但不会影响执行流程.

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)
})
Run Code Online (Sandbox Code Playgroud)

then()函数中,首先解析/拒绝将影响执行流程.所以我的问题是 - 它为什么会这样(功能/错误)?

PS我的环境是Node 4.1

PPS我的输出:

Status #1: Promise { <pending> }
Resolve 1
Value after: 1
Status #2: Promise { 1 }
Resolve 2
Status #3: Promise { 1 }
Run Code Online (Sandbox Code Playgroud)

the*_*eye 8

根据ECMAScript 2015规范,Promise Reject FunctionsPromise Resolve Functions部分说,

  1. 如果alreadyResolved.[[value]]为true,则返回undefined.

因此,如果当前的promise对象已经解决,那么既不解析也不拒绝对Promise对象做任何事情.它实际上意味着,只有第一个解决/拒绝问题.


Ben*_*aum 7

好吧,我想谈谈原因.Promise是单个值的代理,因此第二次运行处理程序或更改值是没有意义的.例如,您无法将数字5更改为数字3.

让我们谈谈我们resolve第二次被召唤的替代方案.假设我们不想允许它 - 我们如何发出信号呢?

通常情况下,我们最好throw-问题是-它会被抓无处因为throw在承诺构造小号地转化为拒绝..catch处理程序无法运行,因为承诺已经解决.

所以我们不能真正抛出,因为这意味着你无法处理的异常(一个非常糟糕的地方).我们不能运行两次处理程序(这会破坏模型).所以我们唯一的选择是允许它.