lib/core/qutil.js
function forEachPromise_p(array, iterator, accept, defaultValue) {
  var deferred = Q.defer();
  var i = 0;
  function tryNext() {
    if (i >= array.length) {
      // We've reached the end of the list--give up
      deferred.resolve(defaultValue);
    }
    else {
      try {
        // Try the next item in the list
        iterator(array[i++])
        .then(
          function(result) {
            // If the promise returns a result, see if it is acceptable; if
            // so, we're done, otherwise move on to the next item in the list
            if (accept(result)) {
              deferred.resolve(result);
            } else {
              tryNext();
            }
          },
          function(err) {
            deferred.reject(err);
          }
        );
      } catch(ex) {
        deferred.reject(ex);
      }
    }
  }
  tryNext();
  return deferred.promise;
}
function forEachPromise_p(array, iterator, accept, defaultValue) {
  var deferred = Q.defer();
  var i = 0;
  function tryNext() {
    if (i >= array.length) {
      // We've reached the end of the list--give up
      deferred.resolve(defaultValue);
    }
    else {
      try {
        // Try the next item in the list
        iterator(array[i++])
        .then(
          function(result) {
            // If the promise returns a result, see if it is acceptable; if
            // so, we're done, otherwise move on to the next item in the list
            if (accept(result)) {
              deferred.resolve(result);
            } else {
              tryNext();
            }
          },
          function(err) {
            deferred.reject(err);
          }
        );
      } catch(ex) {
        deferred.reject(ex);
      }
    }
  }
  tryNext();
  return deferred.promise;
}
function forEachPromise_p ( array , iterator , accept , defaultValue ) { var deferred = Q . defer ( ) ; var i = 0 ; function tryNext ( ) { if ( i >= array . length ) { // We've reached the end of the list--give up deferred . resolve ( defaultValue ) ; } else { try { // Try the next item in the list iterator ( array [ i ++ ] ) . then ( function ( result ) { // If the promise returns a result, see if it is acceptable; if // so, we're done, otherwise move on to the next item in the list if ( accept ( result ) ) { deferred . resolve ( result ) ; } else { tryNext ( ) ; } } , function ( err ) { deferred . reject ( err ) ; } ) ; } catch ( ex ) { deferred . reject ( ex ) ; } } } tryNext ( ) ; return deferred . promise ; }
Starting at the beginning of the array, pass an element to the iterator,
which should return a promise. If the promise resolves, test the value
using the accept function. If accepted, that value is the result. If not
accepted, move on to the next array element.

If any of the iterator-produced promises fails, then reject the overall
promise with that error.

If the end of the array is reached without any values being accepted,
defaultValue is the result.
Starting at the beginning of the array pass an element to the iterator which should return a promise . If the promise resolves test the value using the accept function . If accepted that value is the result . If not accepted move on to the next array element .