是否可以以诺言来返回响应(没有异步/等待)
为什么这不起作用(我借给 >):
callWs2 = function(){
let url = 'https://jsonplaceholder.typicode.com/posts/1';
fetch(url)
.then(function(response) {
return response.text();
})
}
console.log(callWs2());
Why this doesn't work (I borrowed to https://jsfiddle.net/xlanglat/tyh6jjpy/):
callWs2 = function(){
let url = 'https://jsonplaceholder.typicode.com/posts/1';
fetch(url)
.then(function(response) {
return response.text();
})
}
console.log(callWs2());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
。然后
方法返回Promise
,在您的情况下,它将返回Promise
,该可以通过任何response> response..text()解决。注意:
response.text()
还返回Promise
。现在,您需要
返回
此promise
从您的功能中进行。最后,当您调用功能时,需要用
。然后。
.then
method returns aPromise
and in your case it returns aPromise
that resolves with whateverresponse.text()
resolves with. Note:response.text()
also returns aPromise
.Now, you need to
return
thisPromise
from your function.And finally when you're calling the function you need to chain it with
.then
because the function returns aPromise
.