1

I have the following common code to invoke AJAX requests. My question is if the "return" keyword is necessary for the $.ajax (since doing $.ajax would anyways return a promise) OR if it is for some other purpose ?

doXhr: function(postData, requestUrl){  
    var promise = new Ember.RSVP.Promise(function(resolve, reject) {        
        return $.ajax({ //Is the "return" required on this line ?
            url: requestUrl,

        })
        .done(function(response, status, xhrObject) {         
          resolve(response);
        })
        .fail(function(xhrObject, status, error){           
            reject(errorObj);
        });
    })  
    return promise;
},
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • This is the [explicit promise construction antipattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it). *Dont do this*. You can just do `const promise = Ember.RSVP.resolve($.ajax({url: requestUrl}))` – Lux Aug 12 '18 at 23:32

1 Answers1

2

just like other promise libraries (Native, Bluebird, etc), the resolve, reject functions are actually callbacks, so there is no need for a return inside the new Promise. (If you never call resolve or reject though, your promise will be pending forever).

The only return that is needed in the return of the RSVP.Promise like what you have at the bottom -- though only if you are awaiting or thening doXhr

Also, neat side-tip:

With the the more recent ember, you can just do:

import { Promise } from 'rsvp';

new Promise((resolve, reject) => {
  // stuff
});

Edit

for how the function body of the promise get's executed, you'll need to look at the constructor of the Promise: https://github.com/tildeio/rsvp.js/blob/master/lib/rsvp/promise.js#L143

the resolver function (the function you define when making a new promise) is passed to here https://github.com/tildeio/rsvp.js/blob/master/lib/rsvp/-internal.js#L231 and then evaluated.

Hope this helps!

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • Thanks a lot...Just one more question; Whenever this line is encountered, var promise = new Ember.RSVP.Promise .....the code inside the constructor will automatically get invoked...meaning we do not have no invoke the constructor function explicitly...How does that work ? – copenndthagen Aug 12 '18 at 09:14
  • if I understand you correctly, I think that's just a javascript weirdness around how classes are implemented (as functions instead of first _class_ citizens (there are trade-offs to this approach)). The same behavior happens when you do `Array(1, 2)` or `new Array(1, 2)` – NullVoxPopuli Aug 12 '18 at 10:25
  • Is the above you stated with reference to ES5 or ES6 standard ? Actually I was just trying to understand how does new Ember.RSVP.Promise() get executed immediately...meaning without any explicit call to it like callMyConstructorFunction() – copenndthagen Aug 12 '18 at 16:30
  • ah ok. the constructor of the promise executes the function you pass to it :) – NullVoxPopuli Aug 12 '18 at 23:25
  • testndtv, I've updated my answer with some details. and re: ES5 vs ES6, I don't actually remember. It's been a while since I've written vanilla ES5, buuuut, Chrome's console natively supports arrow functions. – NullVoxPopuli Aug 12 '18 at 23:28