Promise callbacks returning promises

后端 未结 4 786
谎友^
谎友^ 2020-12-12 16:30

With regard to these great two sources: NZakas - Returning Promises in Promise Chains and MDN Promises, I would like to ask the following:

Each time that we return a

4条回答
  •  一生所求
    2020-12-12 16:49

    In this example, p2 is a promise. p3 is also a promise originating from p1's fulfillment handler. However p2 !== p3. Instead p2 somehow magically resolves to 43 (how?) and that value is then passed to p3's fulfillment handler. Even the sentence here is confusing.

    a simplified version how this works (only pseudocode)

    function resolve(value){
        if(isPromise(value)){
            value.then(resolve, reject);
        }else{
            //dispatch the value to the listener
        }
    }
    

    the whole thing is quite more complicated since you have to take care, wether the promise has already been resolved and a few more things.

提交回复
热议问题