当 Response.text() promise 会被拒绝?
When Response.text() promise will reject?
我看到 MDN/Response/text 文档显示了仅将 .text()
与 then
一起使用的示例
response.text().then(function (text) {
// do something with the text response
});
It returns a promise that resolves with a String.
因为lint规则,我需要把
// eslint-disable-next-line @typescript-eslint/no-floating-promises
res.text().then(async (t) => {
是否有我需要捕捉来自 Response.text()
的拒绝承诺的用例?也许有一些例子?
如果响应已经 consumed
/读取,它可以 fail/reject,也就是说,如果已经调用了 .text/.json 等等。
查看 polyfill 实现 (https://github.com/github/fetch/blob/d1d09fb8039b4b8c7f2f5d6c844ea72d8a3cefe6/fetch.js#L301 ) 不过我没有看到其他可能的情况。
示例:
response.text()
.then(t1 => {
console.log({ t1 }); // after calling text() we can see the result here
return response; // but we decided to return the response to the next handler
})
.then(res =>res.text()) // here we try to read text() again
.then(t2 => console.log({ t2 })) // and expecting text to be logged here
.catch(er => console.log({ er })); // but the text() promise rejects with
// TypeError: Failed to execute 'text' on 'Response': body stream already read
我看到 MDN/Response/text 文档显示了仅将 .text()
与 then
response.text().then(function (text) {
// do something with the text response
});
It returns a promise that resolves with a String.
因为lint规则,我需要把
// eslint-disable-next-line @typescript-eslint/no-floating-promises
res.text().then(async (t) => {
是否有我需要捕捉来自 Response.text()
的拒绝承诺的用例?也许有一些例子?
如果响应已经 consumed
/读取,它可以 fail/reject,也就是说,如果已经调用了 .text/.json 等等。
查看 polyfill 实现 (https://github.com/github/fetch/blob/d1d09fb8039b4b8c7f2f5d6c844ea72d8a3cefe6/fetch.js#L301 ) 不过我没有看到其他可能的情况。
示例:
response.text()
.then(t1 => {
console.log({ t1 }); // after calling text() we can see the result here
return response; // but we decided to return the response to the next handler
})
.then(res =>res.text()) // here we try to read text() again
.then(t2 => console.log({ t2 })) // and expecting text to be logged here
.catch(er => console.log({ er })); // but the text() promise rejects with
// TypeError: Failed to execute 'text' on 'Response': body stream already read