Created
November 19, 2010 18:45
-
-
Save subtleGradient/706936 to your computer and use it in GitHub Desktop.
sg_asyncEach is like each, but async!
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
| // // // // // // // // // // // // // // // // // // // // // | |
| // works as a generic! | |
| Array.sg_asyncEach([1, 2, 3], | |
| function(value, key, array) { | |
| console.log(value, key, array) | |
| return "eachFn result " + value | |
| }, | |
| function(err, result) { | |
| console.log('sg_asyncEach callback', err && "Error!" || "Success!", result) | |
| } | |
| ) | |
| // works as a method! | |
| ;[1, 2, 3].sg_asyncEach( | |
| function(value, key, array) { | |
| console.log(value, key, array) | |
| return "eachFn result " + value | |
| }, | |
| function(err, result) { | |
| console.log('sg_asyncEach callback', err && "Error!" || "Success!", result) | |
| } | |
| ) | |
| // error handling! | |
| Array.sg_asyncEach([1, 2, 3] | |
| ,function(){throw new Error("FAIL on purpose!")} | |
| ,function(err){if (err) throw err} | |
| ) | |
| // Works on objects and arrays! | |
| Object.sg_asyncEach({a:'a', b:'b', c:'c'}, | |
| function(value, key, array) { | |
| console.log(value, key, array) | |
| return "eachFn result " + value | |
| }, | |
| function(err, result) { | |
| console.log('sg_asyncEach callback', err && "Error!" || "Success!", result) | |
| } | |
| ) | |
| // // // // // // // // // // // // // // // // // // // // // | |
| // Works on MooTools Elements collections if MooTools is in your environment! | |
| if (typeof $$ != 'undefined'){ | |
| new Element('div', {html:"<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol<p>Lol"}) | |
| .inject(document.body) | |
| $$('p') | |
| .setStyle('color', '#f00') | |
| .sg_asyncEach( | |
| function(el, i, elements) { | |
| window.T = window.T || +new Date | |
| el.setStyle('color', '#0f0') | |
| return el | |
| }, | |
| function(err, el) { | |
| el.appendText(' sg_asyncEach:' + (new Date - window.T) + 'ms') | |
| } | |
| ) | |
| } |
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
| /* | |
| --- | |
| name : sg_asyncEach | |
| description : sg_asyncEach is like each, but async! | |
| authors : Thomas Aylott | |
| copyright : © 2010 Thomas Aylott | |
| license : MIT | |
| provides : [sg_asyncEach, Array.sg_asyncEach, Object.sg_asyncEach, Elements.sg_asyncEach] | |
| ... | |
| */ | |
| ;(function(){ | |
| var call = Function.call | |
| function sg_asyncEachItem | |
| ( fn | |
| , value | |
| , key | |
| , object | |
| , callback | |
| , THIS | |
| ){ | |
| if (THIS == null) THIS = this | |
| var result | |
| setTimeout(function(){ | |
| try { | |
| result = | |
| call.call | |
| ( fn | |
| , THIS | |
| , value | |
| , key | |
| , object | |
| ) | |
| callback(false, result, object) | |
| } | |
| catch (error){ | |
| callback(error, result, object) | |
| } | |
| }, | |
| 0); | |
| } | |
| function Object_sg_asyncEach(object, fn, callback, THIS){ | |
| for (var property in object) | |
| sg_asyncEachItem.call | |
| ( this | |
| , fn | |
| , object[property] | |
| , property | |
| , object | |
| , callback | |
| , THIS | |
| ) | |
| return object | |
| } | |
| Object.sg_asyncEach = Object_sg_asyncEach | |
| function Array_sg_asyncEach(array, fn, callback, THIS){ | |
| for (var i = 0, l=array.length; i < l; ++i) | |
| sg_asyncEachItem.call | |
| ( this | |
| , fn | |
| , array[i] | |
| , i | |
| , array | |
| , callback | |
| , THIS | |
| ) | |
| return array | |
| } | |
| Array.sg_asyncEach = Array_sg_asyncEach | |
| Array.prototype.sg_asyncEach = function(fn, callback, THIS){ | |
| return Array_sg_asyncEach.call(this, this, fn, callback, THIS) | |
| } | |
| if (Elements && Elements.implement) | |
| Elements.implement('sg_asyncEach', Array.prototype.sg_asyncEach) | |
| }()) |
Author
Ahh, sweet, I'll check that out. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did an async invoke (it allows (a)sync) for benchmarkjs, waits for async testing to finish before going to the next benchmark and allows early exiting of the loop.
https://github.com/mathiasbynens/benchmark.js/blob/master/benchmark.js#L416-490
Not sure how handy setting the timeout to
0ms is though but async array methods are coo :D