Skip to content

Instantly share code, notes, and snippets.

@petamoriken
Created December 1, 2015 03:14
Show Gist options
  • Save petamoriken/e76622b94bf64dadad11 to your computer and use it in GitHub Desktop.
Save petamoriken/e76622b94bf64dadad11 to your computer and use it in GitHub Desktop.
また新たなクソを生み出してしまったな。
"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);
};
@petamoriken
Copy link
Author

例によって Babel ってどうぞ

[1, 1, 4, 5, 1, 4].sleepSort().then((val) => {
  console.log(val);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment