有一个承诺的功能。在这个函数里面,我再次调用这个函数(递归)。如何等到递归承诺得到解决?

时间:2018-02-21 02:38:06

标签: javascript recursion promise es6-promise

我需要逐个运行两个功能:getUserPlaylists(接收播放列表)和getPlaylistTracks(接收提供的播放列表的曲目)。

一个响应最多可以包含50个曲目,因此如果我想获得其余曲目,我需要使用PageToken。问题是我无法使递归函数getPlaylistTracks等到递归完成。

function getPlaylistsWithTracks () {
  return new Promise((resolve, reject) => {
    getUserPlaylists()
      .then(function (playlists) {

        playlists.forEach(
          async function (playlistObj) {
            await getPlaylistTracks(playlistObj).then(function (tracks) {
              playlistObj['tracks'] = tracks
            })
          })

        console.log('resolve')
        resolve(playlists)
      })
  })
}

function getPlaylistTracks (playlistObj, pageToken) {
  return new Promise((resolve, reject) => {

    let playlistTracks = []

    let requestOptions = {
      'playlistId': playlistObj['youtubePlaylistId'],
      'maxResults': '50',
      'part': 'snippet'
    }

    if (pageToken) {
      console.log('pageToken:', pageToken)
      requestOptions.pageToken = pageToken
    }

    let request = gapi.client.youtube.playlistItems.list(requestOptions)

    request.execute(function (response) {
      response['items'].forEach(function (responceObj) {
        let youtubeTrackTitle = responceObj.snippet.title

        if (youtubeTrackTitle !== 'Deleted video') {
          let youtubeTrackId = responceObj.snippet.resourceId.videoId

          playlistTracks.push({
            youtubePlaylistId: playlistObj.playlistId,
            youtubePlaylistTitle: playlistObj.playlistTitle,
            youtubeTrackId: youtubeTrackId,
            youtubeTrackTitle: youtubeTrackTitle,
          })
        }

      })

      // Here I need to wait a bit
      if (response.result['nextPageToken']) {
        getPlaylistTracks(playlistObj, response.result['nextPageToken'])
          .then(function (nextPageTracks) {
            playlistTracks = playlistTracks.concat(nextPageTracks)
          })
      }

    })

    resolve(playlistTracks)

  })
}

getPlaylistsWithTracks()

在我的控制台案例中,我看到了下一个:

> resolve
> pageToken: 123
> pageToken: 345

但是,我希望resolve看到最后一个。

如何等待递归执行?

1 个答案:

答案 0 :(得分:2)

正确避免使用Promise constructor antipattern(don't) use forEach with async functions

此外,递归没有什么特别之处。这就像你想要等待的任何其他承诺返回函数调用 - 将它放在then链或await中。 (后者相当容易)。

async function getPlaylistsWithTracks() {
  const playlists = await getUserPlaylists();
  for (const playlistObj of playlists) {
    const tracks = await getPlaylistTracks(playlistObj);
    playlistObj.tracks = tracks;
  }
  console.log('resolve')
  return playlists;
}

async function getPlaylistTracks(playlistObj, pageToken) {
  let playlistTracks = []
  let requestOptions = {
    'playlistId': playlistObj['youtubePlaylistId'],
    'maxResults': '50',
    'part': 'snippet'
  }
  if (pageToken) {
    console.log('pageToken:', pageToken)
    requestOptions.pageToken = pageToken
  }
  let request = gapi.client.youtube.playlistItems.list(requestOptions)
  const response = await new Promise((resolve, reject) => {
    request.execute(resolve); // are you sure this doesn't error?
  });

  response['items'].forEach(function (responceObj) {
    let youtubeTrackTitle = responceObj.snippet.title
    if (youtubeTrackTitle !== 'Deleted video') {
      let youtubeTrackId = responceObj.snippet.resourceId.videoId
      playlistTracks.push({
        youtubePlaylistId: playlistObj.playlistId,
        youtubePlaylistTitle: playlistObj.playlistTitle,
        youtubeTrackId: youtubeTrackId,
        youtubeTrackTitle: youtubeTrackTitle,
      })
    }
  })
  if (response.result['nextPageToken']) {
    const nextPageTracks = await getPlaylistTracks(playlistObj, response.result['nextPageToken']);
    playlistTracks = playlistTracks.concat(nextPageTracks);
  }
  return playlistTracks;
}