0

I have an Ember promise call as below;

var promise = new Ember.RSVP.Promise(function(resolve, reject) {
    return $.ajax({
    //want this common
        url: requestUrl,
        type: type, // HTTP method
        dataType: dataType, // type of data expected from the API response
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(postData)
    })
    .done(function(data, status, xhrObject) {
    //But want this to be different
      // resolve call
    })
    .fail(function(xhrObject, status, error){
      // reject call
    });
})

My question is can I use common code for $.ajax(), but have different implementation for done() callback I can check that by passing some parameter from the calling place.

so basically, I want

if (someparam == 'handleDone1')
    call resolve(data)
else
    call resolve({data})
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
  • Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! Just do `var promise = Ember.RSVP.Promise.resolve($.ajax(…));`. – Bergi May 27 '16 at 10:58
  • Regarding your question - there is nothing that stops you from simply placing an `if` statement in that callback, is there? – Bergi May 27 '16 at 10:59
  • yes, i have an external function which has the var promise = new Ember.RSVP.Promise() code...so if I pass any argument say 'someparam' to this external function, I cannot access that in .done(function() {..}) – copenndthagen May 27 '16 at 11:28
  • What do you mean by "external function"? You can access anything that you could access outside the callback also inside of it by closure. – Bergi May 27 '16 at 11:31
  • I get an undefined for that parameter if I try passing it to done...done(function(data, status, xhrObject, paramFromExternalFn)) – copenndthagen May 27 '16 at 11:45
  • I never said you should declare it as a parameter? You should just use it inside. – Bergi May 27 '16 at 11:48
  • even directly accessing it inside the done function gives undefined – copenndthagen May 27 '16 at 11:51
  • Well then it probably *is* undefined. Please [edit] your question to show your full code, without a [mcve] we can't really help you. – Bergi May 27 '16 at 11:57

2 Answers2

1

You are currently passing a function to done by hard coding a function expression into it.

Replace that with a variable. Pass a value to that variable as a function argument.

Alternatively, don't use done here at all. Just return the return value of $.ajax() and call done() on that in the calling function.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

return a promise instead of ajax call. And wrap the ajax call into promise.

checkout the below code. It may helps.

function someFunction(resolve1, reject1) {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    $.ajax({
    //want this common
        url: requestUrl,
        type: type, // HTTP method
        dataType: dataType, // type of data expected from the API response
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(postData)
    })
    .done(function(data, status, xhrObject) {
      //But want this to be different
      // resolve call
      var dicision = resolve1();
      if(dicision){
        resolve(data);
      } else {
        resolve({data});
      }
    })
    .fail(function(xhrObject, status, error){
      // reject call
    });
}
Abu Hanifa
  • 2,857
  • 2
  • 22
  • 38