-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Serializer creates a pipe of consequent promises, resolving with the values | ||
* of each successful promise or rejecting with the last error | ||
* | ||
* @param { array } data Segments for each promise | ||
* @param { function } fn Serializer function | ||
* @return { Promise } Promise | ||
*/ | ||
module.exports = function serializer (tasks, fn) { | ||
tasks = tasks.slice() | ||
let i = 0 | ||
const values = [] | ||
|
||
return new Promise((resolve, reject) => { | ||
const serializer = () => { | ||
if (!tasks.length) { | ||
return resolve(values) | ||
} | ||
|
||
const task = tasks.splice(0, 1)[0] | ||
|
||
Promise.resolve(fn(task, i++)) | ||
.then((response) => { | ||
values.push(response) | ||
serializer() | ||
}) | ||
.catch((err) => { | ||
reject(err) | ||
}) | ||
} | ||
|
||
serializer() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const expect = require('expect.js') | ||
const serializer = require('../lib/serializer') | ||
|
||
describe('serializer', () => { | ||
it('should return a promise', (done) => { | ||
expect(serializer([])).to.be.a(Promise) | ||
done() | ||
}) | ||
|
||
it('should resolve the promises in chain and return all the resolved values as an array', (done) => { | ||
const data = [0, 1, 2, 3] | ||
|
||
serializer(data, (item, i) => { | ||
if (item !== data[i]) { | ||
return Promise.reject(new Error('Did not loop in chain')) | ||
} | ||
|
||
return Promise.resolve(item) | ||
}) | ||
.then((response) => { | ||
expect(response).to.eql(data) | ||
done() | ||
}) | ||
.catch(done) | ||
}) | ||
|
||
it('should stop the serializer chain when callback rejects', (done) => { | ||
const data = [0, 1, 2, 3] | ||
let counter = 0 | ||
|
||
serializer(data, (item, i) => { | ||
if (i === 2) { | ||
return Promise.reject(new Error('Rejecting on purpose')) | ||
} | ||
|
||
counter++ | ||
}) | ||
.then(() => { | ||
done(new Error('Should have rejected the chain')) | ||
}) | ||
.catch((err) => { | ||
if (counter !== 2) { | ||
err.message = 'Counter mismatch' | ||
return done(err) | ||
} | ||
|
||
done() | ||
}) | ||
}) | ||
}) |