1

An array of functions, [fn1,fn2,...], each "returns" through a callback, passing an optional error. If an error is returned through the callback, then subsequent functions in the array should not be called.

// one example function
function fn1( callback ) {
   <process>
   if( error ) callback( errMsg );
   else callback();
   return;
}

// go through each function until fn returns error through callback
[fn1,fn2,fn3,...].forEach( function(fn){
  <how to do this?>
} );

This can be solved other ways, but nonetheless would love the syntactic dexterity to use approach.

Can this be done?


as per correct answer:

[fn1,fn2,fn3,...].every( function(fn) {
  var err;
  fn.call( this, function(ferr) { err = ferr; } );
  if( err ) {
     nonblockAlert( err );
     return false;
  }
  return true;
} );

seems this has room for simplification.

for me, much better approach to solve this type of problem - it's flatter, the logic more accessible.

1 Answer 1

1

If I understand your question correctly and if you can use JavaScript 1.6 (e.g. this is for NodeJS), then you could use the every function.

From MDN:

every executes the provided callback function once for each element present in the array until it finds one where callback returns a false value. If such an element is found, the every method immediately returns false. Otherwise, if callback returned a true value for all elements, every will return true.

So, something like:

[fn1, fn2, fn3, ...].every(function(fn) {
    // process
    if (error) return false;
    return true;
});

Again, this requires JavaScript 1.6

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.