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);
}