From 7f913b6d6a951bef9b287c0e186664b655b90c73 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 6 Apr 2017 22:55:51 -0700 Subject: handle async functions in tryEach --- lib/tryEach.js | 14 ++++++++------ mocha_test/es2017/asyncFunctions.js | 12 ++++++++++++ mocha_test/tryEach.js | 2 +- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/tryEach.js b/lib/tryEach.js index d87245f..025f7c9 100644 --- a/lib/tryEach.js +++ b/lib/tryEach.js @@ -1,6 +1,7 @@ import noop from 'lodash/noop'; import eachSeries from './eachSeries'; -import rest from './internal/rest'; +import wrapAsync from './internal/wrapAsync'; +import slice from './internal/slice'; /** * It runs each task in series but stops whenever any of the functions were @@ -44,14 +45,15 @@ export default function tryEach(tasks, callback) { var result; callback = callback || noop; eachSeries(tasks, function(task, callback) { - task(rest(function (err, args) { - if (args.length <= 1) { - args = args[0]; + wrapAsync(task)(function (err, res/*, ...args*/) { + if (arguments.length > 2) { + result = slice(arguments, 1); + } else { + result = res; } error = err; - result = args; callback(!err); - })); + }); }, function () { callback(error, result); }); diff --git a/mocha_test/es2017/asyncFunctions.js b/mocha_test/es2017/asyncFunctions.js index ca76a56..eb282bc 100644 --- a/mocha_test/es2017/asyncFunctions.js +++ b/mocha_test/es2017/asyncFunctions.js @@ -615,6 +615,18 @@ module.exports = function () { }) }); + it('should handle async functons in tryEach', (done) => { + async.tryEach([ + async () => { throw new Error('fail1'); }, + async () => { throw new Error('fail2'); }, + async () => 5, + async () => { throw new Error('shoult not get here'); } + ], (err, result) => { + expect(result).to.eql(5); + done(); + }) + }); + /** * Utils */ diff --git a/mocha_test/tryEach.js b/mocha_test/tryEach.js index 5e997a6..db884a5 100644 --- a/mocha_test/tryEach.js +++ b/mocha_test/tryEach.js @@ -2,7 +2,7 @@ var async = require('../lib'); var expect = require('chai').expect; var assert = require('assert'); -describe('try', function () { +describe('tryEach', function () { it('no callback', function () { async.tryEach([]); }); -- cgit v1.2.1