Created
October 19, 2016 05:00
-
-
Save dfkaye/fc2bae9158ca5b39240fdb945b1eb2a4 to your computer and use it in GitHub Desktop.
rudimentary non-blocking computation example
This file contains 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
// 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