Promise object used to provide seperation of success and error resolution paths for async operations.

Example
  1. var myFunc = function(){
  2. var promise = new Promise();
  3. //callback the promise after 10 Secs
  4. setTimeout(hitch(promise, "callback"), 10000);
  5. return promise.promise();
  6. }
  7. var myFunc2 = function(){
  8. var promises =[];
  9. for(var i = 0; i < 10; i++){
  10. promises.push(myFunc);
  11. }
  12. //create a new promise list with all 10 promises
  13. return new PromiseList(promises).promise();
  14. }
  15.  
  16. myFunc.then(function(success){}, function(error){})
  17. //chain promise operations
  18. myFunc.chain(myfunc).then(function(success){}, function(error){})
  19.  
  20. myFunc2.then(function(success){}, function(error){})
  21. //chain promise operations
  22. myFunc2.chain(myfunc).then(function(success){}, function(error){})

Constructor

Defined promise.js Source
  1. function (){
  2. this.__errorCbs = [];
  3. this.__cbs = [];
  4. this.callback = hitch(this, this.callback);
  5. this.errback = hitch(this, this.errback);
  6. }

__resolve Function Private


Defined promise.js

Source
  1. function (){
  2. var self = this;
  3. if (!self.__fired) {
  4. self.__fired = true;
  5. nextTick(function () {
  6. var cbs = self.__error ? self.__errorCbs : self.__cbs,
  7. res = self.__error || self.__results,
  8. i = -1, l = cbs.length;
  9. while (++i &lt; l) {
  10. spreadArgs(cbs[i], res);
  11. }
  12. self.__errorCbs.length = self.__cbs.length = 0;
  13. self = null;
  14. });
  15. }
  16. }

addCallback Function Public


Defined promise.js

Add a callback to the callback chain of the promise

Arguments Returns Source
  1. function (cb){
  2. if (cb) {
  3. if (isPromise(cb)) {
  4. cb = cb.callback;
  5. }
  6. if (this.__fired && this.__results) {
  7. this.__callNextTick(cb, this.__results);
  8. } else {
  9. this.__cbs.push(cb);
  10. }
  11. }
  12. return this;
  13. }

addErrback Function Public


Defined promise.js

Add a callback to the errback chain of the promise

Arguments Returns Source
  1. function (cb){
  2. if (cb) {
  3. if (isPromise(cb)) {
  4. cb = cb.errback;
  5. }
  6. if (this.__fired && this.__error) {
  7. this.__callNextTick(cb, this.__error);
  8. } else {
  9. this.__errorCbs.push(cb);
  10. }
  11. }
  12. return this;
  13. }

both Function Public


Defined promise.js

Adds a callback or promise to be resolved for both success and error.

Arguments Returns Source
  1. function (cb){
  2. this.addCallback(cb);
  3. if (isPromise(cb)) {
  4. this.addErrback(cb.callback);
  5. } else {
  6. this.addErrback(cb);
  7. }
  8. return this;
  9. }

callback Function Public


Defined promise.js

When called all functions registered as callbacks are called with the passed in results.

Arguments Source
  1. function (args){
  2. args = argsToArray(arguments);
  3. if (this.__fired) {
  4. throw new Error("Already fired!");
  5. }
  6. this.__results = args;
  7. this.__resolve();
  8. return this.promise();
  9. }

chain Function Public


Defined promise.js

Call to chaining of promises

  1. new Promise()
  2. .callback("hello")
  3. .chain(function(previousPromiseResults){
  4. return previousPromiseResults + " world";
  5. }, errorHandler)
  6. .chain(function(previousPromiseResults){
  7. return when(dbCall());
  8. }).classic(function(err, results){
  9. //all promises are done
  10. });

You can also use static values

  1. new Promise().callback()
  2. .chain("hello")
  3. .chain(function(prev){
  4. return prev + " world!"
  5. }).then(function(str){
  6. console.log(str); //"hello world!"
  7. });

If you do not provide an errback for each chain then it will be propogated to the final promise

  1. new Promise()
  2. .chain(function(){
  3. return new comb.Promise().errback(new Error("error"));
  4. })
  5. .chain(function(){
  6. return prev + " world!"
  7. })
  8. .classic(function(err, str){
  9. console.log(err.message); //"error"
  10. });

Arguments Returns Source
  1. function (callback,errback){
  2. var promise = new Promise(),
  3. self = this;
  4. function _errback(e) {
  5. if (isFunction(errback)) {
  6. try {
  7. var res = spreadArgs(errback, [e]);
  8. isPromiseLike(res) ? res.then(promise.callback, promise.errback) : promise.callback(res);
  9. } catch (e) {
  10. promise.errback(e);
  11. }
  12. } else {
  13. promise.errback(e);
  14. }
  15. }
  16. function _callback() {
  17. try {
  18. var res = isFunction(callback) ? spreadArgs(callback, arguments) : callback;
  19. if (isPromiseLike(res)) {
  20. res.then(promise.callback, _errback);
  21. } else {
  22. promise.callback(res);
  23. promise = null;
  24. }
  25. callback = res = null;
  26. } catch (e) {
  27. _errback(e);
  28. }
  29. }
  30. self.addCallback(_callback);
  31. self.addErrback(_errback);
  32. return promise.promise();
  33. }

chainBoth Function Public


Defined promise.js

Applies the same function that returns a promise to both the callback and errback.

Arguments Returns Source
  1. function (callback){
  2. var promise = new Promise();
  3. this.addCallback(function () {
  4. try {
  5. when(isFunction(callback) ? callback.apply(this, arguments) : callback).then(promise);
  6. } catch (e) {
  7. promise.errback(e);
  8. }
  9. });
  10. this.addErrback(function () {
  11. try {
  12. when(isFunction(callback) ? callback.apply(this, arguments) : callback).then(promise);
  13. } catch (e) {
  14. promise.errback(e);
  15. }
  16. });
  17. return promise.promise();
  18. }

classic Function Public


Defined promise.js

Call this function as a classic node callback where the first argument will be an error, or null if no error occured. The other arugments will be the result from the promise.

Example
  1. promise.classic(function(err, res){
  2. if(err){
  3. console.log(err);
  4. }else{
  5. console.log(res);
  6. }
  7. });
Arguments Returns Source
  1. function (cb){
  2. if ("function" === typeof cb) {
  3. this.addErrback(cb);
  4. this.addCallback(partial(cb, null));
  5. }
  6. return this;
  7. }

errback Function Public


Defined promise.js

When called all functions registered as errbacks are called with the passed in error(s)

Arguments Source
  1. function (args){
  2. if (this.__fired) {
  3. throw args || new Error("Already fired");
  4. }
  5. this.__error = argsToArray(arguments);
  6. this.__resolve();
  7. return this.promise();
  8. }

promise Function Public


Defined promise.js

Creates an object to that contains methods to listen to resolution but not the "callback" or "errback" methods.

Example
  1. var asyncMethod = function(){
  2. var ret = new comb.Promise();
  3. process.nextTick(ret.callback.bind(ret, "hello"));
  4. return ret.promise();
  5. }
  6.  
  7. asyncMethod().callback() //throws error
Returns Source
  1. function (){
  2. var ret = {
  3. promise: function () {
  4. return ret;
  5. }
  6. };
  7. var self = this;
  8. ret["chain"] = function () {
  9. return spreadArgs(self["chain"], arguments, self);
  10. };
  11. ret["chainBoth"] = function () {
  12. return spreadArgs(self["chainBoth"], arguments, self);
  13. };
  14. ret["addCallback"] = function () {
  15. spreadArgs(self["addCallback"], arguments, self);
  16. return ret;
  17. };
  18. ret["addErrback"] = function () {
  19. spreadArgs(self["addErrback"], arguments, self);
  20. return ret;
  21. };
  22. ret["then"] = function () {
  23. spreadArgs(self["then"], arguments, self);
  24. return ret;
  25. };
  26. ret["both"] = function () {
  27. spreadArgs(self["both"], arguments, self);
  28. return ret;
  29. };
  30. ret["classic"] = function () {
  31. spreadArgs(self["classic"], arguments, self);
  32. return ret;
  33. };
  34. return ret;
  35. }

resolve Function Public


Defined promise.js

Resolved a promise using the node style callback.

Example
  1. var promise = new Promise();
  2. fs.readFile("file.txt", "utf8", promise.resolve.bind(promise));
  3. promise.then(function(file){
  4. console.log(file);
  5. });
Arguments Returns Source
  1. function (err,args){
  2. if (err) {
  3. this.errback(err);
  4. } else {
  5. spreadArgs(this.callback, argsToArray(arguments, 1), this);
  6. }
  7. return this;
  8. }

then Function Public


Defined promise.js

Call to specify action to take after promise completes or errors

Arguments Returns Source
  1. function (callback,errback){
  2. if (isPromise(callback)) {
  3. errback = callback.errback;
  4. callback = callback.callback;
  5. }
  6. this.addCallback(callback);
  7. this.addErrback(errback);
  8. return this;
  9. }

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta