简体   繁体   English

为什么我要再创建一个.then从fetch promise中拉出promiseValue

[英]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?? 为什么如果我登录res.text只是一个承诺,而当我再执行一次然后返回相同的return时,接下来.then然后使用相同的res.text就是一个字符串了吗? May someone explain it for me:)? 有人可以帮我解释一下吗:)?

The fetch function returns a Promise that resolves with a Body response. fetch函数返回一个Promise,该Promise通过Body响应进行解析。

Paraphrased from Mozilla: Fetch and Mozilla: Body : Mozilla:FetchMozilla: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. fetch()方法...返回一个Promise,该请求将解析为对该请求的响应,无论请求是否成功...检索到响应后,可以使用多种方法来定义主体内容以及应该如何处理。

If you then navigate to the documentation for Body you can see that it has a number of methods on it. 然后,如果您导航到Body的文档,则可以看到它上面有很多方法。 The Body.text() method: Body.text()方法:

Takes a Response stream and reads it to completion. 获取响应流并读取它以完成操作。 It returns a promise that resolves with a USVString (text). 它返回一个用USVString(文本)解析的承诺。 The response is always decoded using UTF-8. 始终使用UTF-8解码响应。

The behavior you've described is how the fetch API is expected to behave. 您描述的行为是预期fetch API的行为。

The Fetch API returns a promise which resolves to a Response. Fetch API返回一个承诺,该承诺会解析为一个Response。 The Response itself has a text() method which returns a promise as well. Response本身具有text()方法,该方法也返回一个Promise。 You can use this method to read the Response-Stream as a text. 您可以使用此方法以文本形式读取Response-Stream。 See https://developer.mozilla.org/en-US/docs/Web/API/Response 参见https://developer.mozilla.org/en-US/docs/Web/API/Response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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