Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Created October 19, 2016 05:00
Show Gist options
  • Save dfkaye/fc2bae9158ca5b39240fdb945b1eb2a4 to your computer and use it in GitHub Desktop.
Save dfkaye/fc2bae9158ca5b39240fdb945b1eb2a4 to your computer and use it in GitHub Desktop.
rudimentary non-blocking computation example
// 18 Oct 2016
// non-blocking computation
function operate(list, match, callback) {
var start = +new Date()
setTimeout(function next() {
if (list.length == 0) {
callback('done')
callback((+new Date() - start) + 'ms')
return
}
var item = list.pop()
if (item == match) {
callback(item)
list = []
}
setTimeout(next, 0)
}, 0)
}
var array = (function(){
var arr = [], i = 0
while (i < 1000) {
arr.push(i++)
}
return arr
}());
operate(array, 100, function(value) {
console.log(value);
});
/*
100
done
4894ms
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment