-
-
Save haraldmartin/70388 to your computer and use it in GitHub Desktop.
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
function background(iterator, produceNextValue, callbacks) { | |
var value = {}, hasValue = produceNextValue(value); | |
callbacks = callbacks || Object.extend([], { | |
after: function(callback) { | |
this.push(callback); | |
return this; | |
} | |
}); | |
if (hasValue) { | |
iterator(value.value); | |
background.defer(iterator, produceNextValue); | |
} else { | |
callbacks.invoke("call"); | |
} | |
return callbacks; | |
} | |
Object.extend(Array.prototype, { | |
backgroundEach: function(iterator) { | |
var index = 0; | |
return background(iterator, function(memo) { | |
if (index == this.length) { | |
return false; | |
} else { | |
memo.value = this[index++]; | |
return true; | |
} | |
}.bind(this)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment