Created
September 23, 2010 03:56
-
-
Save kriskowal/593066 to your computer and use it in GitHub Desktop.
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
var callbacks = Array.prototype.forEach.call( | |
"abcdefg", | |
function (letter) { | |
// or some expensive computation | |
return letter; | |
} | |
); | |
return callbacks.reduceRight(function (otherwise, callback) { | |
var letter = callback(); | |
if (letter === "h") { | |
return letter; | |
} else { | |
return otherwise(); | |
} | |
}, function () { | |
throw new Error("Can't find H"); | |
}); |
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
A->B->C->D->E->F->G->otherwise | |
| | | | | | | | |
v v v v v v v | |
a b c d e f g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was linked from the Q library examples. I'm not really sure how this solves the problem of breaking out of a promise chain. Can you explain?