Instantly share code, notes, and snippets.

@cdcabrera /deferred.js Secret
Created Mar 21, 2014

Embed
What would you like to do?
Basic rough for implementing a Deferred process.
/*
* referenced jQuery's Deferred pattern
*/
(function(window, undefined){
window.when = function()
{
var _int = function(a)
{
this.success = function(){};
this.failure = function(){};
this.dependents = [];
this.count = 0;
this.fire = 0;
this.stop = false;
var deferreddata = [].slice.call(a,0);
for( var i=0; i < deferreddata.length; i++ )
{
this.count += 1;
if( !deferreddata[i].promise )
{
deferreddata[i] = {value:deferreddata[i]};
}
deferreddata[i].when = this;
this.dependents.push( deferreddata[i] );
}
};
_int.prototype.then = function(s, f)
{
var istype = function(o)
{
return {}.toString.call(o).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
};
if(istype(s) == "function")
{
this.success = s;
}
if(istype(f) == "function")
{
this.failure = f;
}
for( var i=0; i < this.dependents.length; i++ )
{
if( this.dependents[i].value )
{
process.call(this.dependents[i], this.dependents[i].value, 1);
}
}
};
return new _int(arguments);
}
window.deferred = function()
{
var _int = function(a){};
_int.prototype.promise = function()
{
return this;
};
_int.prototype.resolve = function(v)
{
if(!this.when.stop)
{
process.call(this, v);
}
};
_int.prototype.error = function(v)
{
this.fail = true;
process.call(this, v);
};
_int.prototype.cancel = function()
{
this.when.stop = true;
};
return new _int(arguments);
}
function process(v)
{
this.value = v;
var howmany = this.when.count,
howmanyfired = (this.when.fire += 1),
dependents = this.when.dependents,
callback = (this.fail)? this.when.failure : this.when.success;
if( this.fail )
{
this.when.stop = true;
}
if(howmany == howmanyfired || this.fail)
{
var temp = [];
for( var i=0; i < dependents.length; i++ )
{
temp.push(dependents[i].value);
if( this.fail && this===dependents[i] )
{
break;
}
}
callback.apply(this, temp);
}
}
})(this);
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>JS.Deferred</title>
</head>
<body>
<div>
<!-- Begin Body - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<h1>JS.Deferred</h1>
<p>
Sets out a rough Deferred pattern.
</p>
<!-- End Body - - - - - - - - - - - - - - - - - - - - - - - - - - -->
</div>
<script src="deferred.js"></script>
<script>
(function(window, undefined){
//-- use deferred
when( DelayFunc1("one", 5000), DelayFunc2("two", 7000), "three" )
.then(function( one, two, three )
{
console.log("success one = " + Array.prototype.join.call(arguments,","));
},
function( one, two, three )
{
console.log("error one = " + Array.prototype.join.call(arguments,","));
});
//-- no deferred
when( "one", "two", {"three":true} )
.then(function( one, two, three )
{
console.log("success two = " + Array.prototype.join.call(arguments,","));
},
function( one, two, three )
{
console.log("error two = " + Array.prototype.slice.call(arguments,","));
});
//-- delay resolve
function DelayFunc1( str, time )
{
var def = deferred();
setTimeout(function()
{
def.resolve( "Success-"+str );
},time);
return def.promise();
}
//-- delay error
function DelayFunc2( str, time )
{
var def = deferred();
setTimeout(function()
{
def.error( "Error-"+str );
},time);
return def.promise();
}
//--
function StringFormat()
{
var args = Array.prototype.slice.call(arguments, 0),
string = args.shift();
for(var i=0; i<args.length; i++)
{
string = string.replace( new RegExp('\\{'+i+'\\}','g'), args[i]);
}
return string;
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment