Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can jQuery promises be chained with Ember.RSVP.Promise's?

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

like image 410
rsk Avatar asked Sep 15 '13 01:09

rsk


People also ask

Can Promise be chained?

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.

How do you resolve a Promise in Ember?

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

What is chain of promises in Javascript?

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


1 Answers

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

like image 86
Jeremy Green Avatar answered Sep 18 '22 17:09

Jeremy Green