From 93ab5a9e5240d89d25698d576831112342eb58aa Mon Sep 17 00:00:00 2001 From: RobertAKARobin Date: Fri, 7 Dec 2018 14:16:26 -0600 Subject: [PATCH 1/4] chainAsync update: last function shouldn't get 'next' as argument Confident that my tests pass, but _30.js won't update. --- snippets/chainAsync.md | 11 +++++++++-- test/chainAsync.test.js | 26 +++++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/snippets/chainAsync.md b/snippets/chainAsync.md index 10bbf6613..bc6ca1b37 100644 --- a/snippets/chainAsync.md +++ b/snippets/chainAsync.md @@ -6,8 +6,11 @@ Loop through an array of functions containing asynchronous events, calling `next ```js const chainAsync = fns => { - let curr = 0; - const next = () => fns[curr++](next); + let curr = 0, last = fns[fns.length - 1]; + const next = () => { + const fn = fns[curr++] + fn(fn === last ? null : next) + } next(); }; ``` @@ -20,6 +23,10 @@ chainAsync([ }, next => { console.log('1 second'); + setTimeout(next, 1000); + }, + () => { + console.log('2 second'); } ]); ``` diff --git a/test/chainAsync.test.js b/test/chainAsync.test.js index ba640c625..031b88681 100644 --- a/test/chainAsync.test.js +++ b/test/chainAsync.test.js @@ -5,18 +5,30 @@ test('chainAsync is a Function', () => { expect(chainAsync).toBeInstanceOf(Function); }); +let incrementer = 0 test('Calls all functions in an array', () => { + chainAsync([ + next => { + incrementer += 1 + next(); + }, + next => { + incrementer += 1 + next(); + }, + next => { + expect(incrementer).toEqual(2); + } + ]); +}); + +test('Last function does not receive "next" argument', () => { chainAsync([ next => { next(); }, next => { - (() => { - next(); - })(); - }, - next => { - expect(true).toBeTruthy(); + expect(next).toBe(null); } ]); -}); +}) From 881ce6bb04ea1d2710f58fba6344e267ed317476 Mon Sep 17 00:00:00 2001 From: RobertAKARobin Date: Mon, 10 Dec 2018 09:08:02 -0600 Subject: [PATCH 2/4] Added missing semicolons to make linter happy --- test/chainAsync.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/chainAsync.test.js b/test/chainAsync.test.js index 031b88681..44da9fef4 100644 --- a/test/chainAsync.test.js +++ b/test/chainAsync.test.js @@ -5,15 +5,15 @@ test('chainAsync is a Function', () => { expect(chainAsync).toBeInstanceOf(Function); }); -let incrementer = 0 +let incrementer = 0; test('Calls all functions in an array', () => { chainAsync([ next => { - incrementer += 1 + incrementer += 1; next(); }, next => { - incrementer += 1 + incrementer += 1; next(); }, next => { @@ -31,4 +31,4 @@ test('Last function does not receive "next" argument', () => { expect(next).toBe(null); } ]); -}) +}); From 0668b1abdeeda52896e7f0bfa99b429a17eb2b0f Mon Sep 17 00:00:00 2001 From: RobertAKARobin Date: Wed, 12 Dec 2018 09:23:55 -0600 Subject: [PATCH 3/4] Addressed comments by @skatcat31 --- snippets/chainAsync.md | 5 +++-- test/chainAsync.test.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/snippets/chainAsync.md b/snippets/chainAsync.md index bc6ca1b37..2ad3efee3 100644 --- a/snippets/chainAsync.md +++ b/snippets/chainAsync.md @@ -6,10 +6,11 @@ Loop through an array of functions containing asynchronous events, calling `next ```js const chainAsync = fns => { - let curr = 0, last = fns[fns.length - 1]; + let curr = 0; + const last = fns[fns.length - 1]; const next = () => { const fn = fns[curr++] - fn(fn === last ? null : next) + fn === last ? fn(undefined) : fn(next); } next(); }; diff --git a/test/chainAsync.test.js b/test/chainAsync.test.js index 44da9fef4..4a01542c5 100644 --- a/test/chainAsync.test.js +++ b/test/chainAsync.test.js @@ -28,7 +28,7 @@ test('Last function does not receive "next" argument', () => { next(); }, next => { - expect(next).toBe(null); + expect(next).toBe(undefined); } ]); }); From 9e8ccd464144d80cd71775a3a04c05272d390d95 Mon Sep 17 00:00:00 2001 From: RobertAKARobin Date: Wed, 12 Dec 2018 15:53:48 -0600 Subject: [PATCH 4/4] Further addressed comments by @skatcat31 --- snippets/chainAsync.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/chainAsync.md b/snippets/chainAsync.md index 2ad3efee3..5cb3c0985 100644 --- a/snippets/chainAsync.md +++ b/snippets/chainAsync.md @@ -10,7 +10,7 @@ const chainAsync = fns => { const last = fns[fns.length - 1]; const next = () => { const fn = fns[curr++] - fn === last ? fn(undefined) : fn(next); + fn === last ? fn() : fn(next); } next(); };