Skip to content
This repository has been archived by the owner on Mar 25, 2019. It is now read-only.

Commit

Permalink
adjusted shortcut Promise() syntax and shortcut .then() syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
getify committed Feb 23, 2010
1 parent 79550d7 commit a409c0e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions modules/promise.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,20 +41,25 @@ return (function(){
publicAPI = new Promise(); publicAPI = new Promise();


publicAPI.then = function(cb){ publicAPI.then = function(cb){
queue[queue.length] = function(val){ return cb.call(publicAPI,{value:val}); }; if (typeof cb == "function") queue[queue.length] = function(val){ return cb.call(publicAPI,{value:val}); }; // then() callback
else queue[queue.length] = function(val){ return cb; }; // then() value
if (promise_fulfilled) fulfill(old_ret); if (promise_fulfilled) fulfill(old_ret);
return publicAPI; return publicAPI;
}; };


if (cb == null) { if (cb == null) { // empty promise
promise_fulfilled = true; promise_fulfilled = true;
} }
else { else if (typeof cb == "function") { // promise callback
cb.call(publicAPI,{fulfill:function(val){ cb.call(publicAPI,{fulfill:function(val){
promise_fulfilled = true; promise_fulfilled = true;
fulfill.call(publicAPI,val); fulfill.call(publicAPI,val);
},value:undef}); },value:undef});
} }
else { // immediate promise value
promise_fulfilled = true;
return publicAPI.then(cb);
}


return publicAPI; return publicAPI;
}; };
Expand Down

0 comments on commit a409c0e

Please sign in to comment.