return new Promise(function(resolve, reject) { // Bind the iterator to the given parent scope args[args.length - 1] = _.bind(args[args.length - 1], collection); // Add the array and callback to the arguments args.unshift(collection.models); args.push(function(err, result) { if (err) { return reject(err); } resolve(result); }); // Call the async method async[method].apply(async, args); });
CollectionAsync.prototype[method] = function() { var deferred = $.Deferred(); var args = _.toArray(arguments); var collection = this.collection; // Bind the iterator to the given parent scope args[args.length - 1] = _.bind(args[args.length - 1], collection); // Add the array and callback to the arguments args.unshift(collection.models); args.push(function(err, result) { if (err) { return deferred.rejectWith(collection, err); } deferred.resolveWith(collection, result); }); // Call the async method async[method].apply(async, args); return deferred.promise(); }
properties[method] = async.proxy(function() { // poor man's spread to pull out function args, generator, and callback var args = [].slice.call(arguments, 0, arguments.length - 2); var generator = arguments[arguments.length - 2]; var cb = arguments[arguments.length - 1]; if (typeof cb != 'function') { throw new Error('missing callback function'); } if (typeof generator != 'function') { throw new Error('last argument should be a generator function'); } var _args = [] .concat(args) .concat(async.fn(generator)) .concat(cb); _async[method].apply(null, _args); });