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!