summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2017-04-06 22:55:51 -0700
committerAlexander Early <alexander.early@gmail.com>2017-04-06 22:55:51 -0700
commit7f913b6d6a951bef9b287c0e186664b655b90c73 (patch)
treecb3babfc2463b1c3e26a4e81eecbefa84ce16d0b
parent7aa90f39cf8ed4e92a9c939f0521ce1482312c67 (diff)
downloadasync-7f913b6d6a951bef9b287c0e186664b655b90c73.tar.gz
handle async functions in tryEach
-rw-r--r--lib/tryEach.js14
-rw-r--r--mocha_test/es2017/asyncFunctions.js12
-rw-r--r--mocha_test/tryEach.js2
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([]);
});