For a couple of hours I have been trying to figure out how to have a promise in a service in Ember. I really can not understand why it seems to be impossible to not have a service function called if an earlier promise in the chain rejects.
To be more clear, I want to chain several promises which will be located in several different places (services). The simplified, working code, looks as follows:
return new Ember.RSVP.Promise(function(resolve, reject) {
resolve();
}).then(function() {
return new Ember.RSVP.Promise(function (resolve, reject) {
Ember.run.later(function() {
console.log('One');
resolve();
}, 3000);
});
}).then(function () {
return new Ember.RSVP.Promise(function (resolve, reject) {
Ember.run.later(function() {
console.log('Two');
resolve();
}, 3000);
});
}).then(function () {
return new Ember.RSVP.Promise(function (resolve, reject) {
Ember.run.later(function() {
console.log('Three');
resolve();
}, 3000);
});
}).then((resolve) => {
console.log(resolve);
}, (reject) => {
console.log(reject);
});
But when I try to replace the second promise function with a call to a service function containing the exact same promise, it starts to act weird.
return new Ember.RSVP.Promise(function(resolve, reject) {
resolve();
}).then(this.get('demoService.serviceFunction')()).then(function () {
return new Ember.RSVP.Promise(function (resolve, reject) {
Ember.run.later(function() {
console.log('Two');
resolve();
}, 3000);
});
}).then(function () {
return new Ember.RSVP.Promise(function (resolve, reject) {
Ember.run.later(function() {
console.log('Three');
resolve();
}, 3000);
});
}).then((resolve) => {
console.log(resolve);
}, (reject) => {
console.log(reject);
});
In the second case, the demoService.serviceFunction is called regardless of whether the first promise is resolved or rejected. It does not matter if the function is called like this.get('demoService.serviceFunction')() or his.get('demoService').serviceFunction().
I have created a twiddle to demonstrate the problem: http://ift.tt/29DFVdO - The console always seems to be outputting ServiceFunctionCalled, where that should not be the case if the first promise is rejected.
Hopefully somebody can provide me insights on what I am doing wrong here. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire