0

Task: I have a function playlist.expand() that returns a promise with data like [Item1, Item2, Item3]. I have a collection of these playlists and want to concat the data returned from all expand() functions and return it as a flat array. However I am stuck in the following code. I am hoping a few extra pair of eyes could help me spot the error:

Code:

breakdownPlaylist() {
    return this.playlists.reduce( (acc, playlist) => {
        acc.push( playlist.expand().then(result => result.collection));
        return acc;
    }, []);
}

$onInit() {
    console.log('test', this.breakdownPlaylist());
}

Issue: test is equal to [] at the time of execution. I can't understand why.

Shruti Kapoor
  • 1,106
  • 2
  • 12
  • 32
  • 1
    I think there is one missing bracket at line 3 in above code. Can you check that? – Harsh Patel Sep 12 '17 at 06:09
  • Issues around https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call are only part of the problem here, therefore I have voted to reopen. – Roamer-1888 Sep 12 '17 at 10:25
  • I agree. My issue is not returning the value of a promise but combining values returned from multiple promises. – Shruti Kapoor Sep 12 '17 at 17:25
  • hey @HarshPatel, I have checked for missing brackets. There are no syntax errors. The ending concat parenthesis is after `result.collection` – Shruti Kapoor Sep 12 '17 at 17:28
  • Remember that `Array#concat()`, unlike `Array#push()` returns a new array leaving the original array unmodified. That should explains why test is equal to `[]`, though it's not the whole answer. – Roamer-1888 Sep 12 '17 at 18:40
  • 1
    One more reopen vote, and I'll give you a proper solution. – Roamer-1888 Sep 12 '17 at 18:41
  • @Roamer-1888 Thanks for the comment about `Array.push()`. I noticed that last night and now using push instead of concat. – Shruti Kapoor Sep 12 '17 at 18:49

0 Answers0