promised-io
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -22,3 +22,3 @@ /** | ||
} | ||
if(request.timeout === undefined)request.timeout= 20000; // default timeout. | ||
@@ -41,3 +41,3 @@ if(request.url){ | ||
var proxySettings = parse(exports.proxyServer); | ||
request.port = proxySettings.port; | ||
request.port = proxySettings.port; | ||
request.protocol = proxySettings.protocol; | ||
@@ -54,3 +54,3 @@ request.hostname = proxySettings.hostname; | ||
timedOut = true; | ||
client.destroy(); | ||
req.destroy(); | ||
deferred.reject(new Error("Timeout")); | ||
@@ -100,3 +100,6 @@ }, request.timeout); | ||
req.on('error', function(e) { | ||
deferred.reject(e); | ||
if (!timedOut) { | ||
deferred.reject(e); | ||
clearTimeout(timeout); | ||
} | ||
}); | ||
@@ -103,0 +106,0 @@ if(request.body){ |
@@ -27,3 +27,3 @@ /** | ||
else{ | ||
exports[i] = convertNodeAsyncFunction(fs[i]); | ||
exports[i] = convertNodeAsyncFunction(fs[i], i === "readFile"); | ||
} | ||
@@ -30,0 +30,0 @@ } |
{ | ||
"name": "promised-io", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Kris Zyp" |
119
promise.js
@@ -6,5 +6,5 @@ (function(define){ | ||
// this is based on the CommonJS spec for promises: | ||
// this is based on the CommonJS spec for promises: | ||
// http://wiki.commonjs.org/wiki/Promises | ||
// Includes convenience functions for promises, much of this is taken from Tyler Close's ref_send | ||
// Includes convenience functions for promises, much of this is taken from Tyler Close's ref_send | ||
// and Kris Kowal's work on promises. | ||
@@ -21,3 +21,3 @@ // // MIT License | ||
// promise -> given to the consumer | ||
// | ||
// | ||
// A consumer can use the promise | ||
@@ -31,3 +31,3 @@ // promise.then(function(result){ | ||
// | ||
// Alternately, a provider can create a deferred and resolve it when it completes an action. | ||
// Alternately, a provider can create a deferred and resolve it when it completes an action. | ||
// The deferred object a promise object that provides a separation of consumer and producer to protect | ||
@@ -41,3 +41,3 @@ // promises from being fulfilled by untrusted code. | ||
// deferred.promise -> given to the consumer | ||
// | ||
// | ||
// Another way that a consumer can use the promise (using promise.then is also allowed) | ||
@@ -52,3 +52,3 @@ // var when = require("promise").when; | ||
exports.errorTimeout = 100; | ||
exports.errorTimeout = 100; | ||
var freeze = Object.freeze || function(){}; | ||
@@ -72,4 +72,4 @@ | ||
* If an implementation of a promise supports a concurrency model that allows | ||
* execution to block until the promise is resolved, the wait function may be | ||
* added. | ||
* execution to block until the promise is resolved, the wait function may be | ||
* added. | ||
*/ | ||
@@ -95,4 +95,5 @@ /** | ||
Promise.prototype.call = function(functionName /*, args */){ | ||
var fnArgs = Array.prototype.slice.call(arguments, 1); | ||
return this.then(function(value){ | ||
return value[functionName].apply(value, Array.prototype.slice.call(arguments, 1)); | ||
return value[functionName].apply(value, fnArgs); | ||
}); | ||
@@ -146,3 +147,3 @@ }; | ||
return new Deferred(); | ||
} | ||
} | ||
@@ -161,3 +162,3 @@ | ||
}); | ||
exports.currentContext = null; | ||
exports.currentContext = null; | ||
@@ -169,7 +170,7 @@ | ||
var context = exports.currentContext; | ||
function notifyAll(value){ | ||
var previousContext = exports.currentContext; | ||
if(finished){ | ||
throw new Error("This deferred has already been resolved"); | ||
throw new Error("This deferred has already been resolved"); | ||
} | ||
@@ -189,3 +190,3 @@ try{ | ||
for(var i = 0; i < waiting.length; i++){ | ||
notify(waiting[i]); | ||
notify(waiting[i]); | ||
} | ||
@@ -208,3 +209,4 @@ } | ||
if(func){ | ||
handled = true; | ||
handled ? | ||
(handled.handled = true) : (handled = true); | ||
try{ | ||
@@ -224,5 +226,3 @@ var newResult = func(result); | ||
if(isError){ | ||
if (listener.deferred.reject(result, true)) { | ||
handled = true; | ||
} | ||
listener.deferred.reject(result, typeof handled === "object" ? handled : (handled = {})); | ||
} | ||
@@ -238,17 +238,27 @@ else{ | ||
}; | ||
// calling error will indicate that the promise failed | ||
var reject = this.reject = this.errback = this.emitError = function(error, dontThrow){ | ||
var reject = this.reject = this.errback = this.emitError = function(error, handledObject){ | ||
if (typeof handledObject == "object") { | ||
if (handled) { | ||
handledObject.handled = true; | ||
} else { | ||
handled = handledObject; | ||
} | ||
} | ||
isError = true; | ||
notifyAll(error); | ||
if (!dontThrow && typeof setTimeout !== "undefined") { | ||
setTimeout(function () { | ||
if (!handled) { | ||
throw error; | ||
} | ||
}, exports.errorTimeout); | ||
if (!handledObject && typeof setTimeout !== "undefined") { | ||
if (!(typeof handled == "object" ? handled.handled : handled)) { | ||
// set the time out if it has not already been handled | ||
setTimeout(function () { | ||
if (!(typeof handled == "object" ? handled.handled : handled)) { | ||
throw error; | ||
} | ||
}, exports.errorTimeout); | ||
} | ||
} | ||
return handled; | ||
}; | ||
// call progress to provide updates on the progress on the completion of the promise | ||
@@ -258,3 +268,3 @@ this.progress = function(update){ | ||
var progress = waiting[i].progress; | ||
progress && progress(update); | ||
progress && progress(update); | ||
} | ||
@@ -265,3 +275,3 @@ } | ||
var returnDeferred = new Deferred(promise.cancel); | ||
var listener = {resolved: resolvedCallback, error: errorCallback, progress: progressCallback, deferred: returnDeferred}; | ||
var listener = {resolved: resolvedCallback, error: errorCallback, progress: progressCallback, deferred: returnDeferred}; | ||
if(finished){ | ||
@@ -295,3 +305,3 @@ notify(listener); | ||
} | ||
if(canceller){ | ||
@@ -328,3 +338,3 @@ this.cancel = promise.cancel = function(){ | ||
} | ||
} | ||
@@ -336,3 +346,3 @@ /** | ||
function rethrow(err){ throw err; } | ||
/** | ||
@@ -463,3 +473,3 @@ * Registers an observer on a promise, always returning a promise | ||
if(target && typeof target.then === "function"){ | ||
var isFinished, isError, result; | ||
var isFinished, isError, result; | ||
target.then(function(value){ | ||
@@ -497,11 +507,11 @@ isFinished = true; | ||
var deferred = new Deferred(); | ||
if(!(array instanceof Array)){ | ||
if(Object.prototype.toString.call(array) !== '[object Array]'){ | ||
array = Array.prototype.slice.call(arguments); | ||
} | ||
var fulfilled = 0, length = array.length; | ||
var fulfilled = 0, length = array.length, rejected = false; | ||
var results = []; | ||
if (length === 0) deferred.resolve(results); | ||
else { | ||
else { | ||
array.forEach(function(promise, index){ | ||
exports.when(promise, | ||
exports.when(promise, | ||
function(value){ | ||
@@ -514,3 +524,8 @@ results[index] = value; | ||
}, | ||
deferred.reject); | ||
function(error){ | ||
if(!rejected){ | ||
deferred.reject(error); | ||
} | ||
rejected = true; | ||
}); | ||
}); | ||
@@ -550,3 +565,3 @@ } | ||
/** | ||
* Takes an array of promises and returns a promise that is fulfilled when the first | ||
* Takes an array of promises and returns a promise that is fulfilled when the first | ||
* promise in the array of promises is fulfilled | ||
@@ -558,3 +573,3 @@ * @param array The array of promises | ||
var deferred = new Deferred(); | ||
if(!(array instanceof Array)){ | ||
if(Object.prototype.toString.call(array) !== '[object Array]'){ | ||
array = Array.prototype.slice.call(arguments); | ||
@@ -568,3 +583,3 @@ } | ||
deferred.resolve(value); | ||
} | ||
} | ||
}, | ||
@@ -575,3 +590,3 @@ function(error){ | ||
deferred.resolve(error); | ||
} | ||
} | ||
}); | ||
@@ -583,3 +598,3 @@ }); | ||
/** | ||
* Takes an array of asynchronous functions (that return promises) and | ||
* Takes an array of asynchronous functions (that return promises) and | ||
* executes them sequentially. Each funtion is called with the return value of the last function | ||
@@ -602,3 +617,3 @@ * @param array The array of function | ||
deferred.resolve(value); | ||
} | ||
} | ||
} | ||
@@ -687,3 +702,19 @@ next(startingValue); | ||
}; | ||
/** | ||
* Returns a promise. If the object is already a Promise it is returned; otherwise | ||
* the object is wrapped in a Promise. | ||
* @param value The value to be treated as a Promise | ||
* @return A promise wrapping the original value | ||
*/ | ||
exports.as = function(value){ | ||
if (value instanceof Promise) { | ||
return value; | ||
} else { | ||
var ret = defer(); | ||
ret.resolve(value); | ||
return ret; | ||
} | ||
}; | ||
}); | ||
})(typeof define!="undefined"?define:function(factory){factory(require,exports)}); |
@@ -12,2 +12,8 @@ Promised-IO is a cross-platform package for asynchronous promise-based IO. Promises | ||
# Installation | ||
Promised-IO can be installed via npm: | ||
npm install promised-io | ||
# promise | ||
@@ -164,3 +170,3 @@ | ||
This module provides promise-based access to the filesystem. The API of the fs module | ||
basically follows the [Node File System module API](http://nodejs.org/docs/latest/api/fs.html). | ||
basically follows the [Node File System module API](http://nodejs.org/api/fs.html). | ||
Each of the asynchronous functions in the Node's FS API is reflected with a corresponding | ||
@@ -224,7 +230,13 @@ function in the fs module that returns a promise (instead of requiring a callback argument in the initial call). | ||
* forEach | ||
* concat | ||
Additional iterative methods can also access data as it available *and* as it is requested from | ||
the returned lazy array. This means that in order for this function to be excuted, the | ||
resulting array should have a forEach (or any of the other standard methods) called on | ||
it to trigger the request for data. These methods follow this behavior: | ||
* filter | ||
* every | ||
* some | ||
* forEach | ||
* concat | ||
* map | ||
@@ -231,0 +243,0 @@ |
var assert = require("assert"), | ||
all = require("../promise").all, | ||
when = require("../promise").when, | ||
whenPromise = require("../promise").whenPromise, | ||
defer = require("../promise").defer, | ||
Promise = require("../promise"), | ||
Step = require("../step").Step; | ||
@@ -36,2 +38,26 @@ | ||
exports.testMultipleReject = function(){ | ||
all(delayedFail(25), delayedFail(50), delayedSuccess(75)).then(function () { | ||
throw new Error('There should be no success here.'); | ||
}, function () { | ||
// This is where we want to end up, once only. | ||
}); | ||
}; | ||
function delayedSuccess(delay) { | ||
var deferred = defer(); | ||
setTimeout(function () { | ||
deferred.resolve(); | ||
}, delay); | ||
return deferred.promise; | ||
} | ||
function delayedFail(delay) { | ||
var deferred = defer(); | ||
setTimeout(function () { | ||
deferred.reject(); | ||
}, delay); | ||
return deferred.promise; | ||
} | ||
function veryDeferred(){ | ||
@@ -90,4 +116,25 @@ var deferred = defer(); | ||
exports.testAsValue = function (){ | ||
var deferred = Promise.as("supercala"); | ||
deferred.then(function (res) { | ||
assert.ok(res == "supercala"); | ||
}, function () { | ||
throw new Error("Unexpected error"); | ||
}); | ||
}; | ||
exports.testAsPromise = function (){ | ||
var temp = defer(); | ||
temp.resolve("supercala"); | ||
var deferred = Promise.as(temp); | ||
assert.ok(temp === deferred); | ||
deferred.then(function (res) { | ||
assert.ok(res == "supercala"); | ||
}, function () { | ||
throw new Error("Unexpected error"); | ||
}); | ||
}; | ||
if (require.main === module) | ||
require("patr/runner").run(exports); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
89506
2380
287
3