0

I am bulding my project in Ionic2 using Firebase and Angularfire2. In a method I want to wait for multiple Promise to return. So I am using following approch.

deleteAll(param: string[]) {
  return Promise.all([
    this.storage.remove(param[0]),
    this.storage.remove(param[1]),
    this.storage.remove(param[2])
  ]);

But in my case the number of values in param array is not fixed. If i try to use for loop in side method Promise.all its throughing error. Please help me resolve this scenario.

Tapas Mukherjee
  • 2,088
  • 1
  • 27
  • 66
  • 1
    And your code is? And the error is? You just need to transform an array of string into an array of promises, and pass that array to Promise.all(). You don't even need a loop to do that, because JavaScript arrays have a map() method. – JB Nizet Feb 28 '17 at 16:50

1 Answers1

2

You can just use map to convert the string array into an array of promises and then pass that to Promise.all

let promises = param.map(x => this.storage.remove(x));
return Promise.all(promises);
Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
  • I was serching the internet but could not find a proper explanation of what map function actually does. Could you please just tell me in short or provide any link. – Tapas Mukherjee Mar 02 '17 at 17:11
  • If you type `javascript map` in google the first result clearly explains it.... https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map The map() method creates a new array with the results of calling a provided function on every element in this array. – Jesse Carter Mar 02 '17 at 17:44
  • Thanks Jesse. Helped a lot. – Tapas Mukherjee Mar 03 '17 at 03:45