javascript - 如何解决“刚刚评估了以下值。” 对于链式 then 函数
问题描述
解决方案
您正确地使用了 Pomise 来包装第一个 API 调用,但是您没有包装计时器和其他 API 调用,因此承诺在它们完成之前就解决了。现代浏览器支持fetch()
比 更容易使用XMLHttpRequest
,并且它已经返回了一个可以轻松链接的 Promise。现在唯一剩下的就是包装超时:
const delay = ms => new Promise(res => setTimeout(res, ms));
现在我们有了所有这些 Promise,Promise.all
并且可以使用链接来组合它们:
const pokemonList = fetch( "https://pokeapi.co/api/v2/pokemon/?limit=4" ).then(res => res.json());
const pokemonDetails = pokemonList.then(list => {
return delay(3000).then(() => {
return Promise.all(list.results.map(pokemon => {
return fetch(pokemon.url).then(res => res.json());
});
});
});
推荐阅读
- sql - 使用 sql/sequelize 通过 userId 获取喜欢的帖子并获取帖子的点赞数
- date - 如何在ggplots中将日期中断设置为每月的第一天
- javascript - 如何编写 JavaScript Array.from() 映射函数?
- javascript - 我的问题是我想在登录后在母版页布局中启用一个 html 元素,但我不能
- javascript - 获取输入值的 DOM xss 攻击
- html - 悬停图像或文本时如何更改图像和文本。仅使用 CSS
- python - 将二进制分类转换为多标签?
- flutter - Flutter 中的授权
- python - 在使用裸 matplotlib 生成的 Jupyter 笔记本中显示图形
- javascript - 如何对 Y 轴的值进行排序 - Google 图表(服务器端)