首页 > 解决方案 > Promises 链不正确。不返回多个值

问题描述

我想从获取请求中返回两个值:

stats.videoCount
stats.viewCount

使用以下代码,我可以正确返回一个值,但不能同时返回两个值。

以下代码返回视频观看次数。

fetch("https://api.promptapi.com/tiktok/hashtag/planttrees", requestOptions)
  .then(response => response.json())
  .then(treeViewCountResult => console.log(treeViewCountResult.challengeInfo.stats.viewCount))
  .catch(error => console.log('error', error))

但是这段代码不会返回两个值:

.then(response => response.json())
.then(treeVideoResult => console.log(treeVideoResult.challengeInfo.stats.videoCount))
.then(treeViewCountResult => console.log(treeViewCountResult.challengeInfo.stats.viewCount))
.catch(error => console.log('error', error))

treeVideoResult 和 treeViewCountResult 将单独工作,但不能一起工作。

我如何错误地链接承诺?

标签: javascriptpromise

解决方案


它返回单个对象。那你就不需要两个了。

fetch("https://api.promptapi.com/tiktok/hashtag/planttrees", requestOptions)
  .then(response => response.json())
  .then(result => console.log(result.challengeInfo.stats.videoCount, resutl.challengeInfo.stats.viewCount)) 

推荐阅读