I need help to understand some promises logic. Here is what I want to achieve - with jQuery I make get request, then I chain it with my promise which makes some checks, my code:
http://jsbin.com/UVEpurU/1/edit
function checkInfoPromise(r) {
var ok = true,
promise = Ember.RSVP.Promise(function(resolve, reject) {
if(ok) {
return resolve(r);
}
else {
return reject(r);
}
});
return promise;
}
var requestPromise = $.get('/')
.then(function(r){
return checkInfoPromise(r);
});
requestPromise.then(function(r) {
console.log(r);
}).fail(function(r) {
console.log('fail');
});
and it doesn't work as I expect. Documentation says they should work fine with each other, but they don't or my code is incorrect.
Same logic with jQuery+jQuery works fine:
http://jsbin.com/AYeyaxO/1/edit
And Ember+Ember works:
http://jsbin.com/iMUgiDo/1/edit
Example 2: Chaining the Promise with then() Promise resolved You can call multiple functions this way. In the above program, the then() method is used to chain the functions to the promise. The then() method is called when the promise is resolved successfully. You can chain multiple then() methods with the promise.
This second, 'downstream' promise is resolved with the return value of the first promise's fulfillment or rejection handler, or rejected if the handler throws an exception. findUser(). then(function (user) { return user.name; }, function (reason) { return 'default name'; }).
Chaining after a catchIt's possible to chain after a failure, i.e. a catch , which is useful to accomplish new actions even after an action failed in the chain. Read the following example: new Promise((resolve, reject) => { console. log("Initial"); resolve(); }) .
I don't think the two promise implementations are chainable. You'd want to wrap the jquery promise inside of an Ember promise.
var requestPromise = new Ember.RSVP.Promise(function(resolve, reject) {
$.get("/", resolve).fail(reject);
});
JSBin : http://jsbin.com/Ikubiju/1/edit?html,js,console
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With