Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change when API to match events module

Add context tracking
  • Loading branch information...
commit 78c754fd667130611a201f2b8a29bb6482bc7666 1 parent 367a200
@kriszyp kriszyp authored kriskowal committed
Showing with 48 additions and 41 deletions.
  1. +48 −41 lib/promise.js
View
89 lib/promise.js
@@ -114,11 +114,23 @@ DeferredPromise.prototype = Promise.prototype;
exports.defer = function(canceller){
return new Deferred(canceller);
}
+
function Deferred(){
var result, finished, isError, waiting = [], handled, errorTimeout;
var promise = this.promise = new DeferredPromise();
+ var context = exports.context;
function notifyAll(value){
+ if(finished){
+ throw new Error("This deferred has already been resolved");
+ }
+ result = value;
+ finished = true;
+ for(var i = 0; i < waiting.length; i++){
+ notify(waiting[i]);
+ }
+ }
+ function notify(listener){
if(enqueue){
// if we have an event queue, we will enqueue it
enqueue(doNotify);
@@ -126,44 +138,39 @@ function Deferred(){
else{
doNotify();
}
-
- function doNotify(){
- if(finished){
- throw new Error("This deferred has already been resolved");
- }
- result = value;
- finished = true;
- for(var i = 0; i < waiting.length; i++){
- notify(waiting[i]);
- }
- }
- }
- function notify(listener){
- var func = (isError ? listener.error : listener.resolved);
- try{
- if(func){
- handled = true;
- if(typeof errorTimeout == "number"){
- clearTimeout(errorTimeout);
- }
- var newResult = func(result);
- isError = false; // clear it after success
- }
- if(newResult && typeof newResult.then === "function"){
- newResult.then(function(result){
- listener.deferred.resolve(result);
- },
- function(error){
- listener.deferred.reject(error);
- });
- return;
- }
- }
- catch(e){
- isError = true;
- newResult = e;
- }
- listener.deferred[isError ? "reject" : "resolve"](newResult === undefined ? result : newResult);
+
+ function doNotify(){
+ var previousContext = exports.context;
+ exports.context = context;
+ var func = (isError ? listener.error : listener.resolved);
+ try{
+ if(func){
+ handled = true;
+ if(typeof errorTimeout == "number"){
+ clearTimeout(errorTimeout);
+ }
+ var newResult = func(result);
+ isError = false; // clear it after success
+ }
+ if(newResult && typeof newResult.then === "function"){
+ newResult.then(function(result){
+ listener.deferred.resolve(result);
+ },
+ function(error){
+ listener.deferred.reject(error);
+ });
+ return;
+ }
+ }
+ catch(e){
+ isError = true;
+ newResult = e;
+ }
+ finally{
+ exports.context = previousContext;
+ }
+ listener.deferred[isError ? "reject" : "resolve"](newResult === undefined ? result : newResult);
+ }
}
// calling resolve will resolve the promise
this.resolve = function(value){
@@ -233,7 +240,7 @@ function perform(value, async, sync){
* @param progressCallback function to be called when progress is made
* @return promise for the return value from the invoked callback
*/
-exports.when = function(value, resolvedCallback, rejectCallback, progressCallback){
+exports.whenPromise = function(value, resolvedCallback, rejectCallback, progressCallback){
return perform(value, function(value){
return value.then(resolvedCallback, rejectCallback, progressCallback);
},
@@ -250,9 +257,9 @@ exports.when = function(value, resolvedCallback, rejectCallback, progressCallbac
* @return promise for the return value from the invoked callback or the value if it
* is a non-promise value
*/
-exports.whenPreservingType = function(value, resolvedCallback, rejectCallback, progressCallback){
+exports.when = function(value, resolvedCallback, rejectCallback, progressCallback){
if(value && typeof value.then === "function"){
- return exports.when(value, resolvedCallback, rejectCallback, progressCallback);
+ return exports.whenPromise(value, resolvedCallback, rejectCallback, progressCallback);
}
return resolvedCallback(value);
};
Please sign in to comment.
Something went wrong with that request. Please try again.