Skip to content

[Non backward compatible] Making promises more compliant with standards#13

Merged
divdavem merged 1 commit intoariatemplates:masterfrom
divdavem:promisesChanges
Jun 11, 2014
Merged

[Non backward compatible] Making promises more compliant with standards#13
divdavem merged 1 commit intoariatemplates:masterfrom
divdavem:promisesChanges

Conversation

@divdavem
Copy link
Copy Markdown
Member

This pull request makes promises more compliant with standards, cf MDN:

  • It adds support for the promise constructor:
var Promise = require("noder-js/promise");
var promiseInstance = new Promise(function(fulfill, reject){
   // ...
});
  • It removes support for multiple arguments for handlers of then and thenSync. It adds the spread and spreadSync methods to pass the array fulfillment value to the handler as multiple arguments. This changes the API of asyncRequire and request in the following way:

Old usage:

// For the request module:
var request = require("noder-js/request");
request("myFile.txt").then(function (content, xhr) {
   // use content and xhr
});

// For the asyncRequire module:
var asyncRequire = require("noder-js/asyncRequire");
asyncRequire("moduleA","moduleB").then(function(moduleA, moduleB){
   // use moduleA and moduleB
});

New usage:

// For the request module:
var request = require("noder-js/request");
request("myFile.txt").then(function (xhr) {
   // The given parameter is directly the xhr object.
   // The content is now accessible with:
   var content = xhr.responseText;
   // use content and xhr
});

// For the asyncRequire module, it is possible to use either then or spread
var asyncRequire = require("noder-js/asyncRequire");
asyncRequire("moduleA","moduleB").then(function(array) {
   // The given parameter is now an array.
   var moduleA = array[0], moduleB = array[1];
   // use moduleA and moduleB
});

// or

var asyncRequire = require("noder-js/asyncRequire");
asyncRequire("moduleA","moduleB").spread(function(moduleA, moduleB) {
   // use moduleA and moduleB
});
  • This pull request adds the following new methods (and removes the corresponding old ones):

    New method (added) Old method (removed)
    Promise.resolve n/a
    Promise.reject n/a
    Promise.all Promise.when
    Promise.allSettled Promise.whenAll
    promiseInstance.catch n/a
    promiseInstance.finally promiseInstance.always
    promiseInstance.done promiseInstance.end

Note that the then, catch, finally and spread methods have their equivalent thenSync, catchSync, finallySync and spreadSync methods, which call their handler synchronously if the promise is already either fulfilled or rejected at the time the xSync method is called.

  • The promises implementation is now tested with the 2.0.4 (latest) version of promises-aplus-tests. This also means it is now possible to resolve a promise with another promise.

@divdavem divdavem changed the title Making promises more compliant with standards (non backward compatible change). [Non backward compatible] Making promises more compliant with standards Jun 10, 2014
@divdavem divdavem merged commit 5e844cf into ariatemplates:master Jun 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant