Merge pull request #889 from RobertAKARobin/feature/chainasync-updates
[FEATURE] chainAsync update: last function shouldn't get 'next' as argument
This commit is contained in:
@ -7,7 +7,11 @@ Loop through an array of functions containing asynchronous events, calling `next
|
|||||||
```js
|
```js
|
||||||
const chainAsync = fns => {
|
const chainAsync = fns => {
|
||||||
let curr = 0;
|
let curr = 0;
|
||||||
const next = () => fns[curr++](next);
|
const last = fns[fns.length - 1];
|
||||||
|
const next = () => {
|
||||||
|
const fn = fns[curr++]
|
||||||
|
fn === last ? fn() : fn(next);
|
||||||
|
}
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@ -20,6 +24,10 @@ chainAsync([
|
|||||||
},
|
},
|
||||||
next => {
|
next => {
|
||||||
console.log('1 second');
|
console.log('1 second');
|
||||||
|
setTimeout(next, 1000);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
console.log('2 second');
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -5,18 +5,30 @@ test('chainAsync is a Function', () => {
|
|||||||
expect(chainAsync).toBeInstanceOf(Function);
|
expect(chainAsync).toBeInstanceOf(Function);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let incrementer = 0;
|
||||||
test('Calls all functions in an array', () => {
|
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([
|
chainAsync([
|
||||||
next => {
|
next => {
|
||||||
next();
|
next();
|
||||||
},
|
},
|
||||||
next => {
|
next => {
|
||||||
(() => {
|
expect(next).toBe(undefined);
|
||||||
next();
|
|
||||||
})();
|
|
||||||
},
|
|
||||||
next => {
|
|
||||||
expect(true).toBeTruthy();
|
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user