热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

获取返回Promise状态:pending

如何解决《获取返回Promise状态:pending》经验,为你挑选了1个好方法。

我使用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());
});

我认为问题是因为异步/同步方法?



1> T.J. Crowder..:

我认为问题变成异步/同步方法?

是.你(大部分)正确地消耗了原始的fetch()承诺,但返回了承诺.所以:text()

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);
}

推荐阅读
author-avatar
hola'thrme
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有