Defined async.js

utilities for working with promises.

array Static Function Public


Defined async.js

Exposes array methods on a comb.Promise.

The methods added are.

When using this method each of the methods are chainable so you can combine actions.

var array = comb.async.array;
function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

array(asyncArr())
     .map(function (num, i) {
         return num * (i + 1);
     }).filter(function (num) {
         return num % 2;
     }).avg().then(function(avg){
         console.log(avg); //11.666666666666666
     });

Arguments Source
function (p){
   var ret;
   if (!p || !p.__isArrayAsync__) {
       ret = merge(when(p), {
           promise: function () {
               return asyncArray(this);
           }
       });
       forEach(methods, function (m) {
           var func = asyncExports[m];
           ret[m] = function () {
               var args = argsToArray(arguments), mRet = new Promise();
               nextTick(function () {
                   func.apply(null, [ret].concat(args)).then(mRet);
               });
               return asyncArray(mRet);
           };
       });
       ret.__isArrayAsync__ = true;
   } else {
       ret = p;
   }
   p = null;
   return ret;
}
    

avg Static Function Public


Defined async.js

Async version of comb.array.avg.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).avg().then(function(avg){
    console.log(avg); //3
})

comb.async.avg(asyncArr()).then(function(avg){
    console.log(avg); //3
})

Arguments Source
function (avgPromise){
   return when(avgPromise).chain(function (result) {
       return avg.call(array, normalizeResult(result));
   });
}
    

cartesian Static Function Public


Defined async.js

Async version of comb.array.cartesian.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2]);
    return ret.promise;
}

comb.async.array(asyncArr()).cartesian([1,2,3]).then(function(avg){
    console.log(avg); //[ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ] ]
})

 comb.async.cartesian(asyncArr(), [1,2,3]).then(function(avg){
    console.log(avg); //[ [ 1, 1 ], [ 1, 2 ], [ 1, 3 ], [ 2, 1 ], [ 2, 2 ], [ 2, 3 ] ]
})

Source
function (){
   return asyncArray(when.apply(null, argsToArray(arguments)).chain(function (result) {
       return cartesian.apply(array, normalizeResult(result).map(function (arg) {
           return isArray(arg) ? arg : [arg];
       }));
   }));
}
    

compact Static Function Public


Defined async.js

Async version of comb.array.compact.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,null,2,null,3,null,4,null,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).compact().then(function(compacted){
    console.log(compacted); //[1,2,3,4,5]
})

comb.async.compact(asyncArr()).then(function(compacted){
    console.log(compacted); //[1,2,3,4,5]
})

Arguments Source
function (arr){
   return asyncArray(when(arr).chain(function (result) {
       return compact.call(array, normalizeResult(result));
   }));
}
    

difference Static Function Public


Defined async.js

Async version of comb.array.difference.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).difference([3,4,5]).then(function(diff){
    console.log(diff); //[1,2]
})

comb.async.difference(asyncArr(), [3,4,5]).then(function(diff){
    console.log(diff); //[1,2]
})

Source
function (){
   return asyncArray(when.apply(null, argsToArray(arguments)).chain(function (result) {
       return difference.apply(array, normalizeResult(result).map(function (arg) {
           return isArray(arg) ? arg : [arg];
       }));
   }));
}
    

every Static Function Public


Defined async.js

Loops through the results of an promise resolving with true if every item passed, false otherwise. The promise can return an array or just a single item.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.every(asyncArr(), function(item){
     return item <= 5;
}).then(function(every){
     console.log(every); //true
});

You may also return a promise from the iterator block.

comb.async.every(asyncArr(), function(item, index){
    var ret = new comb.Promise();
    process.nextTick(function(){
        ret.callback(item == 1);
    });
    return ret.promise();
}).then(function(){
    console.log(myNewArr) //false;
})

Arguments Returns Source
function (promise,iterator,scope,limit){
   return asyncArray(asyncLoop(promise, iterator, scope, limit).chain(function (results) {
       return results.loopResults.every(function (res) {
           return !!res;
       });
   }));
}
    

filter Static Function Public


Defined async.js

Loops through the results of an promise resolving with the filtered array. The promise can return an array or just a single item.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.filter(asyncArr(), function(item){
     return item % 2;
}).then(function(arr){
     console.log(arr); //[1,3,5];
});

You may also return a promise from the iterator block.

comb.async.filter(asyncArr(), function(item, index){
    var ret = new comb.Promise();
    process.nextTick(function(){
        ret.callback(item % 2);
    });
    return ret.promise();
}).then(function(){
    console.log(myNewArr) //[1,3,5];
})

Arguments Returns Source
function (promise,iterator,scope,limit){
   return asyncArray(asyncLoop(promise, iterator, scope, limit).chain(function (results) {
       var loopResults = results.loopResults, resultArr = results.arr;
       return (isArray(resultArr) ? resultArr : [resultArr]).filter(function (res, i) {
           return loopResults[i];
       });
   }));
}
    

flatten Static Function Public


Defined async.js

Async version of comb.array.flatten.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [[1],[2],[3],[4],[5]]);
    return ret.promise;
}

comb.async.array(asyncArr()).flatten().then(function(flat){
    console.log(flat); //[1,2,3,4,5]
});

comb.async.flatten(asyncArr()).then(function(flat){
    console.log(flat); //[1,2,3,4,5]
});

Source
function (){
   return asyncArray(when.apply(null, argsToArray(arguments)).chain(function (result) {
       return flatten.apply(array, normalizeResult(result).map(function (arg) {
           return isArray(arg) ? arg : [arg];
       }));
   }));
}
    

forEach Static Function Public


Defined async.js

Loops through the results of an promise. The promise can return an array or just a single item.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.forEach(asyncArr(), function(){
     //do something with it
}).then(function(arr){
     console.log(arr); //[1,2,3,4,5];
});

You may also return a promise from the iterator block.

var myNewArr = [];

comb.async.forEach(asyncArr(), function(item, index){
    var ret = new comb.Promise();
    process.nextTick(function(){
        myNewArr.push([item, index]);
        ret.callback();
    });
    return ret.promise();
}).then(function(){
    console.log(myNewArr) //[[1,0], [2,1], [3,2], [4,3], [5,4]]
});

Arguments Returns Source
function (promise,iterator,scope,limit){
   return asyncArray(asyncLoop(promise, iterator, scope, limit).chain(function (results) {
       return results.arr;
   }));
}
    

intersect Static Function Public


Defined async.js

Async version of comb.array.intersect.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).intersect([3,4], [3]).then(function(intersection){
    console.log(intersection); //[3]
});

comb.async.intersect(asyncArr(), [3,4]).then(function(intersection){
    console.log(intersection); //[3,4]
});

Source
function (){
   return asyncArray(when.apply(null, argsToArray(arguments)).chain(function (result) {
       return intersect.apply(array, normalizeResult(result).map(function (arg) {
           return isArray(arg) ? arg : [arg];
       }));
   }));
}
    

invoke Static Function Public


Defined async.js

Async version of comb.array.invoke.

function person(name, age) {
     return {
         getName:function () {
             return when(name);
         },
         getOlder:function () {
             age++;
             return when(this);
         },
         getAge:function () {
             return when(age);
         }
     };
}

var arr = [when(person("Bob", 40)), when(person("Alice", 35)), when(person("Fred", 50)), when(person("Johnny", 56))];
console.log(comb.async.invoke(arr, "getName")); //["Bob", "Alice", "Fred", "Johnny"]
console.log(array(arr).invoke("getOlder").pluck("getAge")) //[41, 36, 51, 57]

Arguments Source
function (arrPromise){
   var args = argsToArray(arguments, 1);
   return asyncArray(when(arrPromise).chain(function (result) {
       return when(invoke.apply(array, [normalizeResult(result)].concat(args)));
   }));
}
    

map Static Function Public


Defined async.js

Loops through the results of an promise resolving with the return value of the iterator function. The promise can return an array or just a single item.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.map(asyncArr(), function(item){
     return item * 2;
}).then(function(arr){
     console.log(arr); //[2,4,6,8,10];
});

You may also return a promise from the iterator block.

comb.async.map(asyncArr(), function(item, index){
    var ret = new comb.Promise();
    process.nextTick(function(){
        ret.callback(item * 2);
    });
    return ret.promise();
}).then(function(){
    console.log(myNewArr) //[2,4,6,8,10];
});

Arguments Returns Source
function (promise,iterator,scope,limit){
   return asyncArray(asyncLoop(promise, iterator, scope, limit).chain(function (results) {
       return results.loopResults;
   }));
}
    

max Static Function Public


Defined async.js

Async version of comb.array.max.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).max().then(function(max){
    console.log(max); //5
})

comb.async.max(asyncArr()).then(function(max){
    console.log(max); //5
})

Source
function (){
   var args = argsToArray(arguments), last = args.pop(), cmp = null;
   if (isFunction(last) || isString(last)) {
       cmp = last;
   } else {
       args.push(last);
   }
   return when.apply(null, args).chain(function (result) {
       return max.call(array, normalizeResult(result), cmp);
   });
}
    

min Static Function Public


Defined async.js

Async version of comb.array.min.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).min().then(function(min){
    console.log(min) //3
});

comb.async.min(asyncArr()).then(function(min){
    console.log(min) //3
});

Source
function (){
   var args = argsToArray(arguments), last = args.pop(), cmp = null;
   if (isFunction(last) || isString(last)) {
       cmp = last;
   } else {
       args.push(last);
   }
   return when.apply(null, args).chain(function (result) {
       return min.call(array, normalizeResult(result), cmp);
   });
}
    

multiply Static Function Public


Defined async.js

Async version of comb.array.multiply.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).multiply(2).then(function(mult){
   console.log(mult); //[1,2,3,4,5,1,2,3,4,5]
});

comb.async.multiply(asyncArr(),2 ).then(function(mult){
   console.log(mult); //[1,2,3,4,5,1,2,3,4,5]
});

Source
function (){
   var args = argsToArray(arguments), last = args.pop(), times = null;
   if (isNumber(last)) {
       times = last;
   } else {
       args.push(last);
   }
   return asyncArray(when.apply(null, args).chain(function (result) {
       return multiply.call(array, normalizeResult(result), times);
   }));
}
    

permutations Static Function Public


Defined async.js

Async version of comb.array.permutations.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3]);
    return ret.promise;
}

comb.async.array(asyncArr()).permutations().then(function(permutations){
     console.log(permutations) //[ [ 1, 2, 3 ],
                               //  [ 1, 3, 2 ],
                               //  [ 2, 3, 1 ],
                               //  [ 2, 1, 3 ],
                               //  [ 3, 1, 2 ],
                               //  [ 3, 2, 1 ] ]
});

comb.async.permutations(asyncArr()).then(function(permutations){
     console.log(permutations) //[ [ 1, 2, 3 ],
                               //  [ 1, 3, 2 ],
                               //  [ 2, 3, 1 ],
                               //  [ 2, 1, 3 ],
                               //  [ 3, 1, 2 ],
                               //  [ 3, 2, 1 ] ]
});

Source
function (){
   var args = argsToArray(arguments), last = args.pop(), times = null;
   if (isNumber(last)) {
       times = last;
   } else {
       args.push(last);
   }
   return asyncArray(when.apply(null, args).chain(function (result) {
       return permutations.call(array, normalizeResult(result), times);
   }));
}
    

pluck Static Function Public


Defined async.js

Async version of comb.array.pluck.

var when = comb.when,
    array = comb.async.array;
function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [
                                             {name:{first:when("Fred"), last:"Jones"}, age:when(50), roles:["a", "b", "c"]},
                                             {name:{first:"Bob", last:"Yukon"}, age:40, roles:when(["b", "c"])},
                                             {name:{first:"Alice", last:"Palace"}, age:when(35), roles:["c"]},
                                             {name:{first:when("Johnny"), last:"P."}, age:56, roles:when([])}
                                            ]);
    return ret.promise;
}

array(asyncArr()).pluck("name.first").then(function(values){
    console.log(values); //["Fred", "Bob", "Alice", "Johnny"]
});

pluck(asyncArr(), "age").then(function(values){
    console.log(values); //[50, 40, 35, 56]
});

Arguments Source
function (arrPromise,property){
   var args = argsToArray(arguments, 1);
   return asyncArray(when(arrPromise).chain(function (result) {
       var prop = property.split(".");
       result = normalizeResult(result);
       return asyncArray(prop).forEach(function (prop) {
           var exec = prop.match(/(\w+)\(\)$/);
           return asyncArray(result).map(function (item) {
               return exec ? item[exec[1]]() : item[prop];
           }, 1).chain(function (res) {
                   result = res;
               });
       }, 1).chain(function () {
               return result;
           });
   }));
}
    

powerSet Static Function Public


Defined async.js

Async version of comb.array.powerSet.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).powerSet().then(function(set){
     console.log(set); //[ [], [ 1 ], [ 2 ], [ 1, 2 ], [ 3 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]
});

comb.async.powerSet(asyncArr()).then(function(set){
     console.log(set); //[ [], [ 1 ], [ 2 ], [ 1, 2 ], [ 3 ], [ 1, 3 ], [ 2, 3 ], [ 1, 2, 3 ] ]
});

Arguments Source
function (arr){
   return asyncArray(when(arr).chain(function (result) {
       return powerSet.call(array, normalizeResult(result));
   }));
}
    

removeDuplicates Static Function Public


Defined async.js

Async version of comb.array.removeDuplicates.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,2,3,3,3,4,4,4]);
    return ret.promise;
}

comb.async.array(asyncArr()).removeDuplicates().then(function(unique){
     console.log(unique); // [1, 2, 3, 4]
});

comb.async.removeDuplicates(asyncArray()).then(function(unique){
     console.log(unique); // [1, 2, 3, 4]
});

Arguments Source
function (arr){
   return asyncArray(when(arr).chain(function (result) {
       return removeDuplicates.call(array, normalizeResult(result));
   }));
}
    

rotate Static Function Public


Defined async.js

Async version of comb.array.rotate.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).rotate(2).then(function(rotated){
    console.log(rotated); // [ 3, 4, 5, 1, 2 ]
});

comb.async.rotate(asyncArr(), 2).then(function(rotated){
    console.log(rotated); // [ 3, 4, 5, 1, 2 ]
});

Source
function (){
   var args = argsToArray(arguments), last = args.pop(), times = null;
   if (isNumber(last)) {
       times = last;
   } else {
       args.push(last);
   }
   return asyncArray(when.apply(null, args).chain(function (result) {
       return rotate.call(array, normalizeResult(result), times);
   }));
}
    

some Static Function Public


Defined async.js

Loops through the results of an promise resolving with true if some items passed, false otherwise. The promise can return an array or just a single item.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.some(asyncArr(), function(item){
     return item == 1;
}).then(function(every){
     console.log(every); //true
});

You may also return a promise from the iterator block.

comb.async.some(asyncArr(), function(item, index){
    var ret = new comb.Promise();
    process.nextTick(function(){
        ret.callback(item > 5);
    });
    return ret.promise();
}).then(function(){
    console.log(myNewArr) //false;
})

Arguments Returns Source
function (promise,iterator,scope,limit){
   return asyncArray(asyncLoop(promise, iterator, scope, limit).chain(function (results) {
       return results.loopResults.some(function (res) {
           return !!res;
       });
   }));
}
    

sort Static Function Public


Defined async.js

Async version of comb.array.sort.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,3,2,5,4]);
    return ret.promise;
}

comb.async.array(asyncArr()).sort().then(function(sorted){
   console.log(sorted); //[1,2,3,4,5]
});

comb.async.sort(asyncArr()).then(function(sorted){
   console.log(sorted); //[1,2,3,4,5]
});

Source
function (){
   var args = argsToArray(arguments), last = args.pop(), cmp = null;
   if (isFunction(last) || isString(last)) {
       cmp = last;
   } else {
       args.push(last);
   }
   return asyncArray(when.apply(null, args).chain(function (result) {
       return sort.call(array, normalizeResult(result), cmp);
   }));
}
    

sum Static Function Public


Defined async.js

Async version of comb.array.sum.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).sum().then(function(sum){
    console.log(sum) //15
})

comb.async.sum(asyncArr()).then(function(sum){
    console.log(sum) //15
})

Arguments Source
function (arr){
   return when(arr).chain(function (result) {
       return sum.call(array, normalizeResult(result));
   });
}
    

transpose Static Function Public


Defined async.js

Async version of comb.array.transpose.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [[1, 2, 3], [4, 5, 6]]);
    return ret.promise;
}

comb.async.array(asyncArr()).transpose().then(function(transposed){
    console.log(transposed); //[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
});

comb.async.transpose(asyncArr()).then(function(transposed){
    console.log(transposed); //[ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
});

Arguments Source
function (arr){
   return asyncArray(when(arr).chain(function (result) {
       return transpose.call(array, normalizeResult(result));
   }));
}
    

union Static Function Public


Defined async.js

Async version of comb.array.union.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).union([3],[7], [9,10]).then(function(union){
     console.log(union); //[1,2,3,4,5,7,9,10]
});

comb.async.union(asyncArr(), [3],[7], [9,10]).then(function(union){
     console.log(union); //[1,2,3,4,5,7,9,10]
});

Source
function (){
   return asyncArray(when.apply(null, argsToArray(arguments)).chain(function (result) {
       return union.apply(array, (normalizeResult(result)).map(function (arg) {
           return isArray(arg) ? arg : [arg];
       }));
   }));
}
    

unique Static Function Public


Defined async.js

Async version of comb.array.unique.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,2,3,3,4,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).unique().then(function(unique){
    console.log(unique); //[1,2,3,4,5]
});

comb.async.unique(asyncArr()).then(function(unique){
    console.log(unique); //[1,2,3,4,5]
});

Source
function (){
   return asyncRemoveDuplicates.apply(null, arguments);
}
    

valuesAt Static Function Public


Defined async.js

Async version of comb.array.valuesAt.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.array(asyncArr()).valuesAt(2,3,4).then(function(values){
    console.log(values); //[3,4,5]
});

comb.async.valuesAt(asyncArr(), 2,3,4).then(function(values){
    console.log(values); //[3,4,5]
});

Arguments Source
function (arrPromise){
   var args = argsToArray(arguments, 1);
   return asyncArray(when(arrPromise).chain(function (result) {
       return when(valuesAt.apply(array, [normalizeResult(result)].concat(args)));
   }));
}
    

zip Static Function Public


Defined async.js

Zips results from promises into an array.

function asyncArr(){
    var ret = new comb.Promise();
    process.nextTick(ret.callback.bind(ret, [1,2,3,4,5]);
    return ret.promise;
}

comb.async.zip(asyncArr(), asyncArr()).then(function(zipped){
     console.log(zipped); //[[1,1],[2,2],[3,3], [4,4], [5,5]]
});

comb.async.array(asyncArr()).zip(asyncArr()).then(function(zipped){
     console.log(zipped); //[[1,1],[2,2],[3,3], [4,4], [5,5]]
});

Returns Source
function (){
   return asyncArray(when.apply(null, argsToArray(arguments)).chain(function (result) {
       return zip.apply(array, normalizeResult(result).map(function (arg) {
           return isArray(arg) ? arg : [arg];
       }));
   }));
}
    

License

MIT https://github.com/C2FO/comb/raw/master/LICENSE

Meta