Javascript 返回未定义对象的循环承诺

Javascript 返回未定义对象的循环承诺,javascript,node.js,promise,Javascript,Node.js,Promise,我已经创建了一个处理一系列电视节目的函数,我正在尝试在一个新的数组中获取每个节目的观看进度 我尝试了一个.map,.foreach,一个for循环,带有承诺。所有的,但是如果我把放在之外,它总是返回未定义的 我做错了什么? 我在用电话 trakt.users.watched({ 用户名:profile.user.username, 键入:“显示”, 扩展:“Nosesons” })。然后(watchedshows=>{ 如果(!isEmpty(watchedshows)){ //获取所有

我已经创建了一个处理一系列电视节目的函数,我正在尝试在一个新的数组中获取每个节目的观看进度

我尝试了一个
.map
.foreach
,一个
for
循环,带有
承诺。所有的
,但是如果我把
放在
之外,它总是返回
未定义的

我做错了什么?

  • 我在用电话

trakt.users.watched({
用户名:profile.user.username,
键入:“显示”,
扩展:“Nosesons”
})。然后(watchedshows=>{
如果(!isEmpty(watchedshows)){
//获取所有观看节目的进度
watchedshows.map(元素=>{
返回trakt.shows.progress.watched({
id:element.show.ids.trakt,
隐藏:“假”,
特价:“假”
}).然后(情节进展=>{
//如果有下一集,且上次观看的日期少于一年(放弃未观看的节目)
if(eposodeprogress.next_插曲&(new Date()-new Date(eposodeprogress.last_wasted_at))/31536000000<1{
return element.show.title+'s'+零前缀(eposodeprogress.next_session.season)+'e'+零前缀(eposodeprogress.next_session.number);
}
});
});
}
})。然后(结果=>{
控制台日志(结果);
});
您需要将从
watchedshows.map
创建的承诺与最终的
链接起来。然后(result
,否则
result
函数将在解决上述承诺之前运行。请尝试使用
Promise。改为使用所有

trakt.users.watched({
  username: profile.user.username,
  type: 'shows',
  extended: 'noseasons'
}).then(watchedshows => {
  if (isEmpty(watchedshows)) return;
  //get progress for all watched shows
  return Promise.all( watchedshows.map(element => {
    return trakt.shows.progress.watched({
      id: element.show.ids.trakt,
      hidden: 'false',
      specials: 'false'
    }).then(episodeProgress => {
      //if theres a next episode and last watched date is less than a year (discard unwatch shows)
      if (episodeProgress.next_episode && (new Date() - new Date(episodeProgress.last_watched_at)) / 31536000000 < 1) {
        return element.show.title + ' s' + zeroprefix(episodeProgress.next_episode.season) + 'e' + zeroprefix(episodeProgress.next_episode.number);
      }
    });
  }));
}).then(result => {
  console.log(result);
});
trakt.users.watched({
用户名:profile.user.username,
键入:“显示”,
扩展:“Nosesons”
})。然后(watchedshows=>{
如果(isEmpty(watchedshows))返回;
//获取所有观看节目的进度
返回Promise.all(watchedshows.map)(元素=>{
返回trakt.shows.progress.watched({
id:element.show.ids.trakt,
隐藏:“假”,
特价:“假”
}).然后(情节进展=>{
//如果有下一集,且上次观看的日期少于一年(放弃未观看的节目)
if(eposodeprogress.next_插曲&(new Date()-new Date(eposodeprogress.last_wasted_at))/31536000000<1{
return element.show.title+'s'+零前缀(eposodeprogress.next_session.season)+'e'+零前缀(eposodeprogress.next_session.number);
}
});
}));
})。然后(结果=>{
控制台日志(结果);
});
您需要将从
watchedshows.map
创建的承诺与最终的
链接起来。然后(result
,否则
result
函数将在解决上述承诺之前运行。请尝试使用
Promise。改为使用所有

trakt.users.watched({
  username: profile.user.username,
  type: 'shows',
  extended: 'noseasons'
}).then(watchedshows => {
  if (isEmpty(watchedshows)) return;
  //get progress for all watched shows
  return Promise.all( watchedshows.map(element => {
    return trakt.shows.progress.watched({
      id: element.show.ids.trakt,
      hidden: 'false',
      specials: 'false'
    }).then(episodeProgress => {
      //if theres a next episode and last watched date is less than a year (discard unwatch shows)
      if (episodeProgress.next_episode && (new Date() - new Date(episodeProgress.last_watched_at)) / 31536000000 < 1) {
        return element.show.title + ' s' + zeroprefix(episodeProgress.next_episode.season) + 'e' + zeroprefix(episodeProgress.next_episode.number);
      }
    });
  }));
}).then(result => {
  console.log(result);
});
trakt.users.watched({
用户名:profile.user.username,
键入:“显示”,
扩展:“Nosesons”
})。然后(watchedshows=>{
如果(isEmpty(watchedshows))返回;
//获取所有观看节目的进度
返回Promise.all(watchedshows.map)(元素=>{
返回trakt.shows.progress.watched({
id:element.show.ids.trakt,
隐藏:“假”,
特价:“假”
}).然后(情节进展=>{
//如果有下一集,且上次观看的日期少于一年(放弃未观看的节目)
if(eposodeprogress.next_插曲&(new Date()-new Date(eposodeprogress.last_wasted_at))/31536000000<1{
return element.show.title+'s'+零前缀(eposodeprogress.next_session.season)+'e'+零前缀(eposodeprogress.next_session.number);
}
});
}));
})。然后(结果=>{
控制台日志(结果);
});

。然后
只能在返回承诺的函数上调用。
。然后
只能在返回承诺的函数上调用。哇,工作完美无瑕!非常感谢,它让我更好地理解了承诺是如何工作的。哇,工作完美无瑕!非常感谢,它让我对g.承诺是如何起作用的。