当我对返回值不感兴趣时如何使用 fetch?
How to use fetch when I am not interested in the returned value?
我通常这样使用fetch
:
fetch(`http://some.api.com`)
.then(r => r.json())
.then(r => someFunctionThatUsesR(r))
我最近有一个案例需要调用 API where
- 对结果不感兴趣
- 我需要在完成时调用另一个函数
fetch(`http://some.api.com`)
.then(r => r.text())
.then(r => someFunction())
这有效,但我的 IDE 警告我 r
未被使用。
这让我想到了一个问题:是否有 better/more JSonic JavaScriptic 方式来处理这个问题?(= 所以无需在 then()
s)
之间拖动结果
如果您不使用结果,则不需要 r
参数。您也不需要致电 r.text()
.
fetch(`http://some.api.com`)
.then(() => someFunction());
你可以写
fetch(`http://some.api.com`)
.then(r => r.text())
.then(someFunction)
我通常这样使用fetch
:
fetch(`http://some.api.com`)
.then(r => r.json())
.then(r => someFunctionThatUsesR(r))
我最近有一个案例需要调用 API where
- 对结果不感兴趣
- 我需要在完成时调用另一个函数
fetch(`http://some.api.com`)
.then(r => r.text())
.then(r => someFunction())
这有效,但我的 IDE 警告我 r
未被使用。
这让我想到了一个问题:是否有 better/more JSonic JavaScriptic 方式来处理这个问题?(= 所以无需在 then()
s)
如果您不使用结果,则不需要 r
参数。您也不需要致电 r.text()
.
fetch(`http://some.api.com`)
.then(() => someFunction());
你可以写
fetch(`http://some.api.com`)
.then(r => r.text())
.then(someFunction)