Created
December 1, 2015 03:14
-
-
Save petamoriken/e76622b94bf64dadad11 to your computer and use it in GitHub Desktop.
また新たなクソを生み出してしまったな。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
function sleepSort(array, callback) { | |
array = array || []; | |
callback = callback || noop; | |
const promises = [], ret = []; | |
let retPromise; | |
for(let val of array) { | |
promises.push(new Promise((resolve) => { | |
setTimeout(() => { | |
ret.push(val); | |
resolve(); | |
}, val * 1000); | |
})); | |
} | |
retPromise = Promise.all(promises) | |
.then(() => { | |
return ret; | |
}); | |
retPromise.then(callback); | |
return retPromise; | |
} | |
function noop() {} | |
Array.prototype.sleepSort = function(callback) { | |
return sleepSort(this, callback); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
例によって Babel ってどうぞ。