admin管理员组

文章数量:1279091

I found that is possible (in ES6 promises, while Promise object is created) to use multiple resolve/reject which will affect PromiseStatus only once but not affect 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() functions first resolve/reject will affect execution flow. So my question is - why it works like this (feature/bug)?

P.S. My env is Node 4.1

P.P.S. My output:

Status #1: Promise { <pending> }
Resolve 1
Value after: 1
Status #2: Promise { 1 }
Resolve 2
Status #3: Promise { 1 }

I found that is possible (in ES6 promises, while Promise object is created) to use multiple resolve/reject which will affect PromiseStatus only once but not affect 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() functions first resolve/reject will affect execution flow. So my question is - why it works like this (feature/bug)?

P.S. My env is Node 4.1

P.P.S. My output:

Status #1: Promise { <pending> }
Resolve 1
Value after: 1
Status #2: Promise { 1 }
Resolve 2
Status #3: Promise { 1 }
Share Improve this question edited Aug 29, 2016 at 2:45 Bergi 665k161 gold badges1k silver badges1.5k bronze badges asked Dec 23, 2015 at 12:22 Michael PlakhovMichael Plakhov 2,2912 gold badges19 silver badges21 bronze badges 21
  • 8 not feature, not bug, there's nothing in the specification of Promise/A+ to suggest that a subsequent attempt to resolve or reject is in any way an error, just that once fulfilled/rejected, the promise remains fulfilled/rejected with an immutable value/reason – Jaromanda X Commented Dec 23, 2015 at 12:25
  • 1 Whats the console output you're seeing given your code example? – An0nC0d3r Commented Dec 23, 2015 at 12:25
  • I think you misunderstand the key principle of Promise - then callbacks are only fired once, when the Promise is resolved (either fulfilled or rejected). And that's the case here, you can easily check it in the console; even though resolve(2) line is executed, the corresponding then argument function is not invoked. – raina77ow Commented Dec 23, 2015 at 12:31
  • 1 did not allow to execute after resolve - that doesn't sound possible - can't see how that could even be written in javascript – Jaromanda X Commented Dec 23, 2015 at 12:54
  • 1 Well, that's not how Promises work or can work. They're all normal Javascript functions, a function execution can't be killed externally. All a promise does is allow you to synchronise the execution of two dependent asynchronous pieces of code (have then wait until another piece of code gives the signal). Nothing more, nothing less. Doesn't mean the signaller is or should be killed. – deceze Commented Dec 23, 2015 at 13:16
 |  Show 16 more ments

2 Answers 2

Reset to default 8

Well, I want to talk about the why. Promises are proxies for single values so running handlers a second time or changing the value doesn't make sense. You can't change the number 5 to be the number 3 for instance.

Let's talk about what alternatives we have for resolve being called a second time. Let's say we didn't want to allow it - how would we signal that?

Normally, we'd throw - the problem is - it would be caught nowhere since throws in the promise constructor get converted to rejections. .catch handlers would not run because the promise already resolved.

So we can't really throw as that'd mean an exception you can't handle (a very bad place to be). We can't run handlers twice (that would break the model). So our only choice left is to allow it.

As per the ECMAScript 2015 Specification, sections Promise Reject Functions and Promise Resolve Functions say that,

  1. If alreadyResolved.[[value]] is true, return undefined.

So, if the current promise object is already resolved, then neither resolve, nor reject do anything to the Promise object. It actually means that, only the first resolve/reject matter.

本文标签: javascriptWhat is the reason to allow multiple resolvereject in ES6 PromiseStack Overflow