Skip to content

Instantly share code, notes, and snippets.

@jhartikainen
Created January 18, 2012 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhartikainen/1635885 to your computer and use it in GitHub Desktop.
Save jhartikainen/1635885 to your computer and use it in GitHub Desktop.
Example of using Array.prototype.some in a sequential flow control fashion
//following functions would perform some operations, which would either succeed or fail,
//which would be indicated by the function returning true or false
function a() {
//do something which returns true or false
}
function b() {
//do something which returns true or false
}
function c() {
//do something which returns true or false
}
//The functions would need to be called in sequence, but if one of them succeeds, the rest should not be executed.
//We can use some() for this quite nicely:
[a, b, c].some(function(worker) {
var result = worker();
if(result) {
//some action here that needs to happen once one of the ops succeeds
}
return result
})
//some() will keep calling the functions in order until one of them returns true, or until they have all been called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment