JavaScript How can i store Promise instead of Deferred

I have a piece of code, which communicate with iOS native layer by dispatching request with callback ID, and storing Deferred in array. Then when the callback comes, we can resolve or reject this Deferred.

My code:

jsGate = (function () {
    function jsGate() {}
    jsGate._messageCount = 0;
    jsGate._callbackDeferreds = {};
    jsGate.dispatch = function (plugin, method, args) {
        var callbackID, d, message;
        callbackID = this._messageCount;
        message = {
            plugin: plugin,
            method: method,
            "arguments": args,
            callbackID: callbackID
        };
        send(message)
        this._messageCount++;
        d = new Deferred;
        this._callbackDeferreds[callbackID] = d;
        return d.promise;
    };

    jsGate.callBack = function (callbackID, isSuccess, valueOrReason) {
        var d;
        d = this._callbackDeferreds[callbackID];
        if (isSuccess) {
            d.resolve(valueOrReason[0]);
        } else {
            d.reject(valueOrReason[0]);
        }
        return delete this._callbackDeferreds[callbackID];
    };

    return jsGate;
})();

Examples of usage:

jsGate.dispatch("ReadLater", "fetchSomething", []).then(function (value) {
    return console.log(value);
});
return jsGate.dispatch("ReadLater", "asyncError", []).then(function (value) {
    return console.log(value);
}, function (reason) {
    return console.log("Failed: " + reason);
});

What's the best practice for that using A+ Promises?

Answer:1

I'm currently using enyo and I have a function on a component that takes a callback function and makes an ajax call, then calls the callback on success. I can't seem to figure out how to spy on the ...

I'm currently using enyo and I have a function on a component that takes a callback function and makes an ajax call, then calls the callback on success. I can't seem to figure out how to spy on the ...

  1. ajax callback
  2. ajax callback function
  3. ajax datatable callback
  4. call ajax callback
  5. ajax callback error
  6. callback after ajax
  7. callback function ajax not working
  8. jquery ajax callback
  9. html callback ajax
  10. get callback ajax jquery
  11. using callback ajax
  12. call callback function ajax
  13. call back method in ajax
  14. ajax callback in oracle apex
  15. ajax call back button
  16. ajax callback example
  17. callback function in jquery ajax
  18. ajax callback drupal 8
  19. ajax callback drupal 7
  20. ajax callback data

I tried many solutions I found on the web. None worked. I'm trying to make a website scroll horizontally when I'm scrolling vertically. I tried to accomplish this via js - nothing. Then I read that I ...

I tried many solutions I found on the web. None worked. I'm trying to make a website scroll horizontally when I'm scrolling vertically. I tried to accomplish this via js - nothing. Then I read that I ...

I'm writing a node wrapper to interact with an external api and am having a difficult time testing the asynchronous createJob method. Below is the test case code: api_key = "...

I'm writing a node wrapper to interact with an external api and am having a difficult time testing the asynchronous createJob method. Below is the test case code: api_key = "...

  1. mocha test asynchronous function
  2. mocha tests async
  3. mocha test asynchronous callback
  4. mocha test asynchronous code
  5. mocha test asynchronous calls
  6. mocha asynchronous tests
  7. mocha test async function
  8. mocha test async await
  9. mocha test async error
  10. mocha test async method
  11. mocha test async code
  12. mocha test async done
  13. mocha test async promise
  14. mocha test async await function
  15. mocha async tests timeout
  16. mocha async test example
  17. mocha async test order
  18. mocha async test fail
  19. mocha async test case
  20. mocha dynamic tests async

I'm writing a module that will create a dynamic menu on the fly. How to run a directive after adding new <li> with css class dropdown which is also added by ng-class. The code: angular.module('...

I'm writing a module that will create a dynamic menu on the fly. How to run a directive after adding new <li> with css class dropdown which is also added by ng-class. The code: angular.module('...

  1. dynamically adding dropdown using angularjs
  2. dynamically add dropdown in javascript
  3. dynamically add dropdown in jquery
  4. dynamically add dropdown using jquery
  5. dynamically add dropdown on button click javascript
  6. dynamically adding dropdown using jquery
  7. dynamically add dropdown items bootstrap
  8. dynamically adding dropdown using angular 4
  9. dynamically adding dropdown values javascript
  10. dynamically add dropdown in html
  11. dynamically add dropdown mvc
  12. bootstrap dynamically add dropdown
  13. javascript dynamically add dropdown options
  14. jquery dynamically add dropdown options
  15. angularjs dynamically add dropdown