Quick question about promise flattening


#1

So this works:

this.platform.ready()
    .then(() => {
        return Facebook.getLoginStatus();
    })
    .then() ... more promise stuff.

This doesn’t work:

this.platform.ready()
    .then(Facebook.getLoginStatus)
    .then() ... more promise stuff.

Error: TypeError: null is not an object (evaluating ‘t.pluginRef’)

I don’t really understand why this error happens, as getLoginStatus returns a promise. Shouldn’t this work? I hate wrapping functions in anonymous functions, like in the first example. It’s redundant and ugly.


#2

It’s not really about promises, it’s about function invocation and this.

You can write

.then(() => Facebook.getLoginStatus())


#3

I suppose that would work, however Angular2’s own Hero example has this code:

  getHeroes (): Observable<Hero[]> {
    return this.http.get(this.heroesUrl)
                    .map(this.extractData)
                    .catch(this.handleError);
  }

  private extractData(res: Response) {
    let body = res.json();
    return body.data || { };
  }

Notice no arrow functions.


#4

Should I take this to stackoverflow? :slight_smile:


#5

Uh? Is that supposed to be a threat of some sort? :smile: You can ask wherever you want of course. I thought I already provided an answer though.


#6

No no, but notice my reply, where I reference the angular hero tutorial.


#7

Notice that extractData in that example might as well be a static method, in that it doesn’t care what this is. Perhaps that’s not true of the method you are using.