Why does .json() return a promise?

后端 未结 4 2048
小鲜肉
小鲜肉 2020-11-22 04:24

I\'ve been messing around with the fetch() api recently, and noticed something which was a bit quirky.

let url = \"http://jsonplaceholder.typic         


        
4条回答
  •  旧时难觅i
    2020-11-22 04:53

    Also, what helped me understand this particular scenario that you described is the Promise API documentation, specifically where it explains how the promised returned by the then method will be resolved differently depending on what the handler fn returns:

    if the handler function:

    • returns a value, the promise returned by then gets resolved with the returned value as its value;
    • throws an error, the promise returned by then gets rejected with the thrown error as its value;
    • returns an already resolved promise, the promise returned by then gets resolved with that promise's value as its value;
    • returns an already rejected promise, the promise returned by then gets rejected with that promise's value as its value.
    • returns another pending promise object, the resolution/rejection of the promise returned by then will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the value of the promise returned by then will be the same as the value of the promise returned by the handler.

提交回复
热议问题