Repository: caolan/async (js)

Issue:
Async Whilst errors if test() gets false before iteration starts 
<!--
This template is for bug reports. If you are reporting a bug, please continue on. If you are here for another reason (such as a feature request, change, or question) you can disregard this template
-->

**What version of async are you using?**
3.0.1-0 

**Which environment did the issue occur in (Node version/browser version)**
node 8.9.4

**What did you do? Please include a minimal reproducable case illustrating issue.**
Used async whilst in a case where the test was false on the first try, before calling the iterator.

**What did you expect to happen?**
After the test is called, I expected to go to callback()

**What was the actual result?**
Error: undefined is not iterable

From looking at the code, it looks like whilst tries to call spread on result on line 55 if the truth is false. Since next() hasn't been called yet (and won't be called), result is still undefined, so spread gets called on undefined, and the error occurs.


Pull request:
initialize results in whilst [fixes #1626]
Really minor change, but I figured it would be good to get a second set of eyes, at least on the test. Thanks!

Patch:
diff --git a/intro.md b/intro.md
index 4402663..0638077 100644
--- a/intro.md
+++ b/intro.md
@@ -9,7 +9,7 @@
 
 Async is a utility module which provides straight-forward, powerful functions
 for working with asynchronous JavaScript. Although originally designed for
-use with [Node.js](https://nodejs.org/) and installable via `npm install --save async`,
+use with [Node.js](https://nodejs.org/) and installable via `npm install async`,
 it can also be used directly in the browser.
 
 Async is also installable via:
@@ -218,7 +218,7 @@ The source is available for download from
 Alternatively, you can install using npm:
 
 ```bash
-$ npm install --save async
+$ npm install async
 ```
 
 As well as using Bower:
@@ -267,7 +267,7 @@ included in the `/dist` folder. Async can also be found on the [jsDelivr CDN](ht
 We also provide Async as a collection of ES2015 modules, in an alternative `async-es` package on npm.
 
 ```bash
-$ npm install --save async-es
+$ npm install async-es
 ```
 
 ```js
diff --git a/lib/whilst.js b/lib/whilst.js
index 1013d37..361b72d 100644
--- a/lib/whilst.js
+++ b/lib/whilst.js
@@ -40,7 +40,7 @@ function whilst(test, iteratee, callback) {
     callback = onlyOnce(callback);
     var _fn = wrapAsync(iteratee);
     var _test = wrapAsync(test);
-    var results
+    var results = [];
 
     function next(err, ...rest) {
         if (err) return callback(err);


Test patch:
diff --git a/test/whilst.js b/test/whilst.js
index 1c222c9..1ee6f11 100644
--- a/test/whilst.js
+++ b/test/whilst.js
@@ -64,6 +64,24 @@ describe('whilst', () => {
         }, 10)
     });
 
+    it('should not error when test is false on first iteration', (done) => {
+        var counter = 0;
+
+        async.whilst(
+            (cb) => cb(null, false),
+            (cb) => {
+                counter++;
+                cb(null);
+            },
+            (err, result) => {
+                expect(err).to.eql(null);
+                expect(result).to.be.undefined;
+                expect(counter).to.equal(0);
+                done();
+            }
+        );
+    });
+
     it('doWhilst', (done) => {
         var call_order = [];