0

This time I created a Promise.all Call like this. tools.isMember and tools.unsubscribe will return promise objects.

   tools.isMember(userid)
      .then(results => {
        
          Promise.all(
            Object.keys(results).map((key, index) => {
              tools.unsubscribe(results[index], userid)
            })
          )
        
      })
      .then(unsubscribe => { console.log('Unsubscribed Results => ', unsubscribed)})
      .catch(err => console.log(err))

Console prints

Unsubscribed Results => undefined

I try to move the logging up a little to debug and i place the console.log at where tools.unsubscribe is

   tools.isMember(userid)
      .then(results => {
        
          Promise.all(
            Object.keys(results).map((key, index) => {
              tools.unsubscribe(results[index], userid).then(result => { console.log('Result from Tools => " result) }) //Added Logging Here
            })
          )
        
      })
      .then(unsubscribe => { console.log('Unsubscribed Results => ', unsubscribe)})
      .catch(err => console.log(err))

Now console shows

Unsubscribed Results => undefined

Result from Tools => 1

So now I know the promise is returning the expected results from tools.unsubscribed however the Promise.all is supposed to return an array with all the results? It's now showing undefined.

I've tried many different trouble shooting but i'm new to Promise. Trying to figure out what went wrong with Promise.all this time.

Updated @Bergie : Added return on tools.unsubscribe

   tools.isMember(userid)
      .then(results => {
        
          Promise.all(
            Object.keys(results).map((key, index) => {
              tools.unsubscribe(results[index], userid).then(result =>  { return result })
            })
          ).then(result => { return result }) //Tryning to interpret Bergie's answer            
      })
      .then(unsubscribe => { console.log('Unsubscribed Results => ', unsubscribed)})
      .catch(err => console.log(err))

Console prints

Unsubscribed Results => undefined

Community
  • 1
  • 1
Someone Special
  • 12,479
  • 7
  • 45
  • 76
  • 1
    You're not returning the `Promise.all()` promise from the `then` callback so it resolves with `undefined`. – Bergi Apr 22 '18 at 12:43
  • Also you're not returning the `unsubscribe()` promise from the `map` callback so `Promise.all` will only see an array of `undefined`s. – Bergi Apr 22 '18 at 12:44
  • @Bergi can you take a look at my updated codes? That is what you meant right? But the thing is it's still shows undefined. – Someone Special Apr 22 '18 at 13:11
  • No, [`.then(result => { return result })` is pointless](https://stackoverflow.com/q/41089122/1048572). I was saying that there are missing `return` statements in the `results => { Promise.all(…) }` and `(key, index) => {…}` callbacks. – Bergi Apr 22 '18 at 13:36

1 Answers1

1

You need to return a promise from your map, currently you aren't returning anything, which is equivalent to undefined, arrow functions with curly braces around the body block need explicit return, so you either add it or remove the curly braces.

You also don't need the then inside the map, you need to return the result of the unsubscribe() call, which is a Promise itself.

So your code should be:

tools.isMember(userid)
        .then(results => {
            Promise.all(
                Object.keys(results).map((key, index) => tools.unsubscribe(results[index], userid))
            ).then(unsubscribe => { console.log('Unsubscribed Results => ', unsubscribed)}) //unsubscribe is an array
            .catch(err => console.log(err))
        })

Edit:

then() is a method of the Promise object, so you should call it on instances of Promise. See the MDN documentation page for more detail.

What I meant about putting return in your arrow function body or removing the curly braces { and } is that

(x, y) => x + y 
//is equivalent to 
(x, y) => {
   return x + y
}
//while
(x, y) => {
    x + y
}
//is equivalent to
(x, y) => {
    x + y
    return
}
Daniel Alexandrov
  • 1,299
  • 8
  • 13
  • Wow that worked! I guess the place where I put .then is wrong. It's confusing sometimes where to put the next then. Can you show me what you meany by curly braces returned? – Someone Special Apr 22 '18 at 13:42