Skip to content

Instantly share code, notes, and snippets.

@minznerjosh
Created April 25, 2017 14:27
Show Gist options
  • Save minznerjosh/7aa3662d714f751d061988ec87cd7ff6 to your computer and use it in GitHub Desktop.
Save minznerjosh/7aa3662d714f751d061988ec87cd7ff6 to your computer and use it in GitHub Desktop.
function sleepSort(numbers) {
const sorted = [];
return new Promise((resolve) => {
numbers.forEach(num => setTimeout(() => {
sorted.push(num);
// resolve promise when all numbers have been pushed.
if (sorted.length === numbers.length) {
resolve(sorted);
}
}, num));
});
}
sleepSort([8, 42, 38, 111, 2, 39, 1]).then(sorted => console.log(sorted));
@lintaho
Copy link

lintaho commented Apr 25, 2017

✌️

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