Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created September 19, 2015 22:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1995eaton/763983225493bb49ed4c to your computer and use it in GitHub Desktop.
Save 1995eaton/763983225493bb49ed4c to your computer and use it in GitHub Desktop.
Sort an array ES6 style
function sort(array, sortReliability=10) {
'use strict';
return new Promise(resolve => {
let sorted = [];
Promise.all(array.map(e => {
return new Promise(resolve => {
setTimeout(() => {
sorted.push(e);
resolve(e);
}, e * sortReliability);
});
})).then(() => resolve(sorted));
});
}
function* randomArray(min, max, n) {
while (n--)
yield ~~(Math.random() * (max - min)) + min;
}
sort([...randomArray(0, 100, 10)]).then(console.log.bind(console));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment