Created
April 25, 2017 14:27
-
-
Save minznerjosh/7aa3662d714f751d061988ec87cd7ff6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
✌️