-1

My code:

function getItems(myUrl) {
  let items = []
  request.post(authOptions, function(error, response, body) {
    if (!error && response.statusCode === 200) {

      // use the access token to access the Spotify Web API
      var token = body.access_token;
      var options = {
        url: myUrl,
        headers: {
          'Authorization': 'Bearer ' + token
        },
        json: true
      };
      request.get(options, function(error, response, body) {
        for (var item of body['items']) {
          items.push(item)
        }
        if (response.body.next != null) {
          getItems(response.body.next)
        }
      })
    }
  })
  return items
}
console.log(getItems('https://api.spotify.com/v1/playlists/1vZFw9hhUFzRugOqYQh7KK/tracks?offset=0'))

the function adds data to the list "items" defined within the function. Every time I try to console log the function's return, I get an empty list because it skips directly to return items, returning an empty list. I need a way to make the function wait before returning the list.

Thanks!

  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ivar Jun 12 '20 at 23:40
  • honestly, it probably does, but I have a hard time implementing it. – Kishan Sripada Jun 13 '20 at 00:00

1 Answers1

-1

Since you're having a hard time implementing it, here's the code using Promise.

function getItems(myUrl) {
    return new Promise((resolve, reject) => {
        let items = []
        request.post(authOptions, function (error, response, body) {
            if (!error && response.statusCode === 200) {

                // use the access token to access the Spotify Web API
                var token = body.access_token;
                var options = {
                    url: myUrl,
                    headers: {
                        'Authorization': 'Bearer ' + token
                    },
                    json: true
                };
                request.get(options, function (error, response, body) {
                    if (error) return reject(error);

                    for (var item of body['items']) {
                        items.push(item)
                    }
                    if (response.body.next != null) {
                        getItems(response.body.next)
                    }
                    resolve(items)
                })
            } else {
                reject(error)
            }
        })
        return items
    })
}

// you have to put this inside an async function
console.log(await getItems('https://api.spotify.com/v1/playlists/1vZFw9hhUFzRugOqYQh7KK/tracks?offset=0'))

// or you can use then clause
getItems('https://api.spotify.com/v1/playlists/1vZFw9hhUFzRugOqYQh7KK/tracks?offset=0')
    .then(res => console.log(res))
Duc Nguyen
  • 836
  • 6
  • 12
  • ```console.log(await getItems("https://api.spotify.com/v1/playlists/1vZFw9hhUFzRugOqYQh7KK/tracks"))) ^^^^^ SyntaxError: missing ) after argument list``` – Kishan Sripada Jun 13 '20 at 19:42