Skip to content

Instantly share code, notes, and snippets.

@maxim75
Created Sep 16, 2011
Embed
What would you like to do?
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