How to create a tree of promises?
-
21-12-2019 - |
Question
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);
}
Solution
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));