@shanyue/promise-utils
Version:
Userful promise utils, include map, filter, retry and sleep
64 lines • 2.24 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mocha_1 = require("mocha");
const chai_1 = require("chai");
const time_span_1 = __importDefault(require("time-span"));
const index_1 = require("../index");
const input = [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
4,
5,
6
];
const addOne = async (n) => {
n = await n;
return n + 1;
};
mocha_1.describe('Promise.map', function () {
mocha_1.it('expect work', async () => {
const r = await index_1.map(input, x => addOne(x));
chai_1.expect(r).to.deep.equal([2, 3, 4, 5, 6, 7]);
});
mocha_1.it('expect work with concurrency', async () => {
this.timeout(10000);
const ts = time_span_1.default();
const r = await index_1.map(input, async (x) => {
await index_1.sleep(300);
return addOne(x);
}, { concurrency: 1 });
const time = ts();
chai_1.expect(r).to.deep.equal([2, 3, 4, 5, 6, 7]);
chai_1.expect(time).to.above(1800);
chai_1.expect(time).to.below(2000);
});
mocha_1.it('expect work with concurrency 2', async () => {
this.timeout(10000);
const ts = time_span_1.default();
const r = await index_1.map(input, async (x) => {
await index_1.sleep(300);
return addOne(x);
}, { concurrency: 2 });
const time = ts();
chai_1.expect(r).to.deep.equal([2, 3, 4, 5, 6, 7]);
chai_1.expect(time).to.above(900);
chai_1.expect(time).to.below(1000);
});
mocha_1.it('expect work with concurrency 3', async () => {
this.timeout(10000);
const ts = time_span_1.default();
const r = await index_1.map(input, async (x, i) => {
await index_1.sleep(i * 100);
return addOne(x);
}, { concurrency: 2 });
const time = ts();
chai_1.expect(r).to.deep.equal([2, 3, 4, 5, 6, 7]);
chai_1.expect(time).to.above(900);
chai_1.expect(time).to.below(1000);
});
});
//# sourceMappingURL=map.js.map