Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

Deferred

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Deferred - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

301

index.js

@@ -1,157 +0,170 @@

function _Deferred () {
if ( !( this instanceof _Deferred ) ) return new _Deferred();
var callbacks = [],
fired,
firing,
cancelled,
deferred = this;
this.done = function done (){
if ( !cancelled ) {
var args = arguments,
i = 0,
elem,
type,
_fired;
if ( fired ) {
_fired = fired;
fired = 0;
}
for ( ; elem = args[i++] ; ) {
if ( typeof elem === "object" && elem instanceof Array ){
deferred.done.apply( deferred, elem );
} else if ( typeof elem === "function" ) {
callbacks.push( elem );
(function(define){
define([], function(){
function _Deferred () {
if ( !( this instanceof _Deferred ) ) return new _Deferred();
var callbacks = [],
fired,
firing,
cancelled,
deferred = this;
this.done = function done (){
if ( !cancelled ) {
var args = arguments,
i = 0,
elem,
type,
_fired;
if ( fired ) {
_fired = fired;
fired = 0;
}
for ( ; elem = args[i++] ; ) {
if ( typeof elem === "object" && elem instanceof Array ){
deferred.done.apply( deferred, elem );
} else if ( typeof elem === "function" ) {
callbacks.push( elem );
}
}
if ( _fired ){
deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
}
}
}
if ( _fired ){
deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] );
}
}
return this;
};
this.resolveWith = function resolveWith ( context, args ){
if ( !cancelled && !fired && !firing ) {
args = args || [];
firing = 1;
try {
while( callbacks[0] ) {
callbacks.shift().apply( context, args );
return this;
};
this.resolveWith = function resolveWith ( context, args ){
if ( !cancelled && !fired && !firing ) {
args = args || [];
firing = 1;
try {
while( callbacks[0] ) {
callbacks.shift().apply( context, args );
}
} finally {
fired = [ context, args ];
firing = 0;
}
}
} finally {
fired = [ context, args ];
firing = 0;
}
return this;
};
this.resolve = function resolve () {
deferred.resolveWith( this, arguments );
return this;
};
this.isResolved = function isResolved () {
return !!( firing || fired );
};
this.cancel = function cancel (){
cancelled = 1;
callbacks = [];
return this;
};
}
return this;
};
this.resolve = function resolve () {
deferred.resolveWith( this, arguments );
return this;
};
this.isResolved = function isResolved () {
return !!( firing || fired );
};
this.cancel = function cancel (){
cancelled = 1;
callbacks = [];
return this;
};
}
var promiseMethods = "done fail isResolved isRejected promise then always pipe".split( " " );
var Deferred = module.exports = function Deferred ( func ) {
var deferred = _Deferred(),
failDeferred = _Deferred(),
promise;
deferred.then = function then ( doneCallbacks, failCallbacks ) {
deferred.done( doneCallbacks ).fail( failCallbacks );
return this;
};
deferred.always = function always () {
return deferred.done.apply( deferred, arguments ).fail.apply( this, arguments );
};
deferred.fail = failDeferred.done;
deferred.rejectWith = failDeferred.resolveWith;
deferred.reject = failDeferred.resolve;
deferred.isRejected = failDeferred.isResolved;
deferred.pipe = function pipe ( fnDone, fnFail ) {
return Deferred(function( newDefer ){
var handles = {
done : [fnDone, "resolve"],
fail : [fnFail, "reject"]
var promiseMethods = "done fail isResolved isRejected promise then always pipe".split( " " );
var Deferred = function Deferred ( func ) {
var deferred = _Deferred(),
failDeferred = _Deferred(),
promise;
deferred.then = function then ( doneCallbacks, failCallbacks ) {
deferred.done( doneCallbacks ).fail( failCallbacks );
return this;
};
Object.keys( handles ).forEach( function( handler ){
var data = handles[handler],
fn = data[0],
action = data[1],
returned;
if ( typeof fn === "function" ) {
deferred[ handler ](function(){
returned = fn.apply( this, arguments );
if ( returned && typeof returned.promise === "function" ) {
returned.promise().then( newDefer.resolve, newDefer.reject );
deferred.always = function always () {
return deferred.done.apply( deferred, arguments ).fail.apply( this, arguments );
};
deferred.fail = failDeferred.done;
deferred.rejectWith = failDeferred.resolveWith;
deferred.reject = failDeferred.resolve;
deferred.isRejected = failDeferred.isResolved;
deferred.pipe = function pipe ( fnDone, fnFail ) {
return Deferred(function( newDefer ){
var handles = {
done : [fnDone, "resolve"],
fail : [fnFail, "reject"]
};
Object.keys( handles ).forEach( function( handler ){
var data = handles[handler],
fn = data[0],
action = data[1],
returned;
if ( typeof fn === "function" ) {
deferred[ handler ](function(){
returned = fn.apply( this, arguments );
if ( returned && typeof returned.promise === "function" ) {
returned.promise().then( newDefer.resolve, newDefer.reject );
} else {
newDefer[ action ]( returned );
}
});
} else {
newDefer[ action ]( returned );
deferred[ handler ]( newDefer[ action ] );
}
});
} else {
deferred[ handler ]( newDefer[ action ] );
}, handles)
}).promise();
};
deferred.promise = function ( obj ) {
if ( obj == null ) {
if ( promise ) {
return promise;
}
promise = obj = {};
}
}, handles)
}).promise();
};
deferred.promise = function ( obj ) {
if ( obj == null ) {
if ( promise ) {
return promise;
promiseMethods.forEach(function(method){
this[ method ] = deferred[ method ];
}, obj);
return obj;
};
deferred.done( failDeferred.cancel ).fail( deferred.cancel );
delete deferred.cancel;
if ( func ) {
func.call( deferred, deferred );
}
promise = obj = {};
return deferred;
}
promiseMethods.forEach(function(method){
this[ method ] = deferred[ method ];
}, obj);
return obj;
};
deferred.done( failDeferred.cancel ).fail( deferred.cancel );
delete deferred.cancel;
if ( func ) {
func.call( deferred, deferred );
}
return deferred;
}
module.exports.when = function when ( firstParam ) {
var args = arguments,
i = 0,
length = args.length,
count = length,
deferred = length <= 1 && firstParam && typeof firstParam.promise === "function" ?
firstParam :
Deferred();
function resolveFunc ( i ) {
return function ( value ) {
args[ i ] = arguments.length > 1 ? [].slice.call( arguments, 0 ) : value;
if ( !( --count ) ) {
deferred.resolveWith( deferred, [].slice.call( args, 0 ) );
Deferred.when = function when ( firstParam ) {
var args = arguments,
i = 0,
length = args.length,
count = length,
deferred = length <= 1 && firstParam && typeof firstParam.promise === "function" ?
firstParam :
Deferred();
function resolveFunc ( i ) {
return function ( value ) {
args[ i ] = arguments.length > 1 ? [].slice.call( arguments, 0 ) : value;
if ( !( --count ) ) {
deferred.resolveWith( deferred, [].slice.call( args, 0 ) );
}
};
}
};
}
if ( length > 1 ) {
for ( ; i < length ; i++ ) {
if ( args[ i ] && typeof args[ i ].promise === "function" ){
args[ i ].promise().then( resolveFunc(i), deferred.reject );
} else {
--count;
if ( length > 1 ) {
for ( ; i < length ; i++ ) {
if ( args[ i ] && typeof args[ i ].promise === "function" ){
args[ i ].promise().then( resolveFunc(i), deferred.reject );
} else {
--count;
}
}
if ( !count ) {
deferred.resolveWith( deferred, args );
}
} else if ( deferred !== firstParam ) {
deferred.resolveWith( deferred, length ? [ firstParam ] : [] );
}
return deferred.promise();
}
if ( !count ) {
deferred.resolveWith( deferred, args );
}
} else if ( deferred !== firstParam ) {
deferred.resolveWith( deferred, length ? [ firstParam ] : [] );
return Deferred;
}); // define for AMD if available
})(typeof define !== "undefined" ? define
// try to define as a CommonJS module instead
: typeof module !== "undefined" ? function(deps, factory) {
module.exports = factory();
}
return deferred.promise();
}
// nothing good exists, just define on current context (ie window)
: function(deps, factory) { this.Deferred = factory(); }
);
{
"name" : "Deferred",
"version" : "0.0.3",
"version" : "0.0.4",
"description" : "A port of the jQuery Deferred library to node js.",

@@ -17,3 +17,18 @@ "keywords" : [

"url" : "git://github.com/webspinner/Deferred.git"
},
"contributors" : [
{
"name" : "Gabriel Hernandez",
"email" : "webspinner.gabriel@gmail.com",
"url" : "https://github.com/webspinner"
},
{
"name" : "Ben Loveridge",
"email" : "",
"url" : "https://github.com/bloveridge"
}
],
"devDependencies" : {
"vows" : ">=0.4.1"
}
}
jQuery Deferred Library for Node js.
===========
[website](http://webspinner.github.com/Deferred)
Usage :

@@ -5,0 +5,0 @@

var vows = require("vows"),
assert = require("assert"),
Deferred = require("deferred");
Deferred = require("../index");

@@ -5,0 +5,0 @@ vows.describe('Deferred Object').addBatch({

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc