1

I'm trying to create a tree of promises in Ember.

        return this.store.find('session', 'session').then(function(session) {
            if (session.get('isEmpty')) {
                return this.store.createRecord('session').save().then(function(session) {
                    session.set('id', 'session');

                    return session.save();
                }.bind(this));
            } else {
                return session;
            }
        }.bind(this), function(session) {
            return this.store.createRecord('session').save().then(function(session) {
                session.set('id', 'session');

                return session.save();
            }.bind(this));
        }.bind(this)).then(function(session) {
            this.controllerFor('application').onLanguageChange();

            this.set('localStorage.session', session);

            return session;
        }.bind(this));

I would like to execute the promises as shown. Mention that there are also nested promises createRecord(..).save().then. Is it possible to do that?

It's not exactly a tree of promises here since the last one should be executed for both branches. It could be of course if I put those in an own function. So like this:

'successBranch'.then(function(session) {
   setSessionDependents(session);
   return session;
}

'failBranch'.then(function(session) {
   setSessionDependents(session);
   return session;
}

function setSessionDependents(session) {
    this.controllerFor('application').onLanguageChange();

    this.set('localStorage.session', session);
}
dylanmensaert
  • 1,689
  • 5
  • 24
  • 39

1 Answers1

1

the last one should be executed for both branches

It does actually! If an error handler doesn't throw an exception, the error had been handled and the promise does resolve with the return value of the handler.

Is it possible to do that?

Yes! That's one of the core properties of then, that it resolves with nested promises.

However, you could simplify your code a little bit, as you've got a bunch of duplication in there:

return this.store.find('session', 'session').then(function(session) {
    if (session.get('isEmpty')) {
        throw new Error("no session found");
    else
        return session;
}).then(null, function(err) {
    return this.store.createRecord('session').save().then(function(session) {
        session.set('id', 'session');
        return session.save();
    });
}.bind(this)).then(function(session) {
    this.controllerFor('application').onLanguageChange();
    this.set('localStorage.session', session);
    return session;
}.bind(this));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I read that when working with promises, you should place them behind each other and not within. Is there a way to refactor this code as such? – dylanmensaert Jun 04 '14 at 08:38
  • Chaining instead of nesting I guess? – dylanmensaert Jun 04 '14 at 08:39
  • Or is nesting not a bad practice in this case? Thanks for the provided answer though! I guess I overlooked something during testing. Really appreciate the refactored answer :) – dylanmensaert Jun 04 '14 at 08:42
  • No, a certain level of nesting is always necessary if there are different control flow paths. Apart from that, promises provide the *ability* to flatten pyramids, it's not strictly necessary. In my code you can see 3 chained `.then()` calls, and only one nested. – Bergi Jun 04 '14 at 08:48
  • I think a synchronous example could really help here :) – Benjamin Gruenbaum Jun 04 '14 at 22:24