Does “resolve” consistently mean something distinct from “fulfill”?

前端 未结 2 1827
Happy的楠姐
Happy的楠姐 2021-01-18 05:05

(Related but not quite the same: JS Promises: Fulfill vs Resolve)

I\'ve been trying to wrap my head around Javascript promises, and I\'m struggling with the basic no

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 05:28

    Let's add emphasis somewhere else in one of your quotes :

    A promise is resolved if trying to resolve or reject it has no effect, i.e. the promise has been "locked in" to either follow another promise, or has been fulfilled or rejected

    reject() will change your Promise and all the prior-chained ones status to rejected.
    While resolve() will lock your current Promise, but only the fulfillment of the resolving callbacks will set its status to fulfilled. As long as everything is not finished, your Promise will still be 'pending'.

    e.g If you do chain your Promise, and that during the chain, an error is thrown, then the state of your Promise is set to 'rejected'.

    var p = new Promise((resolve, reject) => {
      resolve(); // here p is resolved, we can't call resolve() or reject() anymore
    }).then((e) => { // chain it
      return new Promise((resolve, reject) => {
        console.log(p); // 'pending' because it's still chained
        reject('whatever') // this one throws an error and breaks the chain
      })
    }).then(() => console.log('passed')); // won't happen
    
    
    setTimeout(() => console.log(p, "please check in your browser's console"), 1000); // rejected

    So even if you are resolve()-ing the current operation of your Promise, you can't know what will happen after, so you can't know if at the end it will be fulfilled or rejected.

提交回复
热议问题