简体   繁体   中英

Why i have make another .then to pull a promiseValue from fetch promise

在此处输入图片说明

Hello. First .then is resolved and have a value response. Why if i log res.text it is just a promise, and when i make another then with the same return , next .then with the same res.text it's a string now?? May someone explain it for me:)?

The fetch function returns a Promise that resolves with a Body response.

Paraphrased from Mozilla: Fetch and Mozilla: Body :

The fetch() method... returns a Promise that resolves to the Response to that request, whether it is successful or not... Once a Response is retrieved, there are a number of methods available to define what the body content is and how it should be handled.

If you then navigate to the documentation for Body you can see that it has a number of methods on it. The Body.text() method:

Takes a Response stream and reads it to completion. It returns a promise that resolves with a USVString (text). The response is always decoded using UTF-8.

The behavior you've described is how the fetch API is expected to behave.

The Fetch API returns a promise which resolves to a Response. The Response itself has a text() method which returns a promise as well. You can use this method to read the Response-Stream as a text. See https://developer.mozilla.org/en-US/docs/Web/API/Response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM