Skip to content

Instantly share code, notes, and snippets.

@maxim75
Created September 16, 2011 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxim75/1221013 to your computer and use it in GitHub Desktop.
Save maxim75/1221013 to your computer and use it in GitHub Desktop.
using deferreds in jQuery
function deferredTest(num, func)
{
var dfd = new $.Deferred();
console.log("deferredTest");
setTimeout(function() {
if(func(num))
{
console.log("resolve");
dfd.resolve( num );
}
else
{
console.log("reject");
dfd.reject( num );
}
}, 5000);
return dfd.promise();
}
$.when(deferredTest(9, function(x) { return (x > 10); }),
deferredTest(14, function(x) { return (x < 15); }))
.done(function(x) { console.log("done = " + x); })
.fail(function(x) { console.log("fail = " + x); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment