Skip to content

Instantly share code, notes, and snippets.

@ajalab
Created May 20, 2011 13:47
Show Gist options
  • Save ajalab/982916 to your computer and use it in GitHub Desktop.
Save ajalab/982916 to your computer and use it in GitHub Desktop.
sleepsort.js
function sleepsort(arr, callback) {
var res = [];
$.when.apply($, a.map(function (n) {
var dfd = $.Deferred();
setTimeout(function () {
res.push(n);
dfd.resolve();
}, n);
return dfd.promise();
})).done(function () {
callback(res);
});
}
/*
usage:
sleepsort([5, 23, 6, 20], function (res) {
console.log(res); // "maybe" [5, 6, 20, 23]
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment