简体   繁体   English

获取返回Promise状态:pending

[英]fetch return Promise state: pending

I test the fetch API with jsonplaceholder URL, but my function returns "Promise State: Pending", and I don't understand why : 我使用jsonplaceholder URL测试fetch API,但我的函数返回“Promise State:Pending”,我不明白为什么:

function getUsers(url) {
  return fetch(url)
}

const users = getUsers(`https://jsonplaceholder.typicode.com/users`);

users.then(response => {
  console.log(response.text());
});

I think the problem is because of asynchronous/synchronous methods? 我认为问题是因为异步/同步方法?

I think the problem become asynchrone/synchrone method ? 我认为问题变成异步/同步方法?

Yes. 是。 You've (mostly) correctly consumed the original fetch() promise, but text() also returns a promise. 你(大部分)正确地使用了原始的fetch() promise,但是text() 返回了一个promise。 So: 所以:

users.then(response => response.text()) // 1
     .then(json => {                    // 2
          console.log(json);
     })
     .catch(error => {                  // 3
          // handle error
     });

 fetch("https://jsonplaceholder.typicode.com/users") .then(response => response.text()) // 1 .then(json => { // 2 log("typeof json: " + typeof json); log(json); }) .catch(error => { // 3 // handle error }); function log(msg) { var p = document.createElement("pre"); p.appendChild(document.createTextNode(msg)); document.body.appendChild(p); } 

At #1 above, we respond to the successful resolution of the fetch() promise by starting the process of reading the body text, returning the promise from text() . 在上面的#1中,我们通过启动读取正文文本的过程,从text()返回promise来响应fetch() promise的成功解析。

At #2 above, we respond to the successful resolution of text() 's promise by using the resulting text (a string containing JSON). 在上面的#2中,我们通过使用结果文本(包含JSON的字符串text()来响应text()的承诺的成功解决。

At #3 above, we handle an error from either the original fetch() or the text() promise by doing something with it. 在上面的#3中,我们通过对它执行某些操作来处理来自原始fetch()text() promise的错误。

Always be sure to handle promise rejections. 务必处理承诺拒绝。 If you don't, they're unlike unhandled exceptions. 如果你不这样做,那么它们与未处理的异常不同。 They're reported to the console, and some environments (like recent versions of Node) terminate on unhandled rejections. 它们被报告给控制台,并且某些环境(如最新版本的Node)终止未处理的拒绝。


Since you're requesting JSON, you might want to use json() rather than text() so you both read and parse it: 由于您正在请求JSON,您可能希望使用json()而不是text()因此您可以读取并解析它:

users.then(response => response.json())
     .then(arrayOfUsers => {
          console.log(arrayOfUsers);
     })
     .catch(error => {
          // handle error
     });

 fetch("https://jsonplaceholder.typicode.com/users") .then(response => response.json()) .then(arrayOfUsers => { log("typeof arrayOfUsers: " + typeof arrayOfUsers); log("arrayOfUsers.length: " + arrayOfUsers.length); }) .catch(error => { // 3 // handle error }); function log(msg) { var p = document.createElement("pre"); p.appendChild(document.createTextNode(msg)); document.body.appendChild(p); } 

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

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