JSFiddle - React, Tailwind, and code Playground
JavaScript
try {
var order = function (x, cb) {
// Call the callback `cb` with x after x * 10 milliseconds
setTimeout(
function () { cb(x); },
x * 10);
};
// This is the world's best sorting algorithm. It works by
// creating a `result` array. Then, it iterates through the
// numbers that need to be sorted. It then sets a timeout so
// that each number will be pushed onto the `result` array
// after 10 times the milliseconds of the number itself. For
// example, 8 would be pushed onto the array after 80 milliseconds,
// 3 would be pushed on after 30 milliseconds, etc. Then, a timeout
// of 10 times the maximum number to be sorted in milliseconds is set. After that
// the callback with the result array is called. For example, if the
// maximum number to be sorted is 15, the result will be returned after
// 150 milliseconds.
var sort = function (numbers, cb) {
// `numbers` is an array of numbers to be sorted
// `cb` is the callback which will be called with the sorted result
// as the only argument
var i,
max = -Infinity,
result = [];
// Iterate over the numbers, and fire off the timeout to
// add it to the array
for (i = 0; i < numbers.length; ++i) {
// Pass in a callback to `order` to add a "sorted" number to
// the result array
order(numbers[i], function (x) { result.push(x); });
// Keep track of the maximum number to be sorted for later
if (numbers[i] > max)
max = numbers[i];
}
// Call the callback with the result array in max * 10 milliseconds.
// All of the numbers should be in the array by then.
setTimeout(function () { cb(result); }, max * 10);
};
sort([5, 2, 6, 9, 3, 4, 2, 10], function (result) {
$('p').append(result.join(','));
});
}
catch (e) {
...