Skip to content

Commit c757f52

Browse files
authored
feat: remove parallel and asyncify as obsolete (#55)
BREAKING CHANGE: removed the `parallel` method. Use native `Promise.all` instead BREAKING CHANGE: removed the `asyncify` method. Call your async method directly
1 parent d19df0f commit c757f52

File tree

2 files changed

+35
-90
lines changed

2 files changed

+35
-90
lines changed

lib/asyncbox.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,6 @@ export async function retryInterval<T = any>(
106106
return await retry(times, wrapped);
107107
}
108108

109-
export const parallel = B.all;
110-
111-
/**
112-
* Fire and forget async function execution
113-
* @param fn - The function to execute asynchronously
114-
* @param args - Arguments to pass to the function
115-
*/
116-
export function asyncify(fn: (...args: any[]) => any | Promise<any>, ...args: any[]): void {
117-
B.resolve(fn(...args)).done();
118-
}
119-
120109
/**
121110
* Similar to `Array.prototype.map`; runs in serial or parallel
122111
* @param coll - The collection to map over
@@ -129,7 +118,7 @@ export async function asyncmap<T, R>(
129118
runInParallel = true,
130119
): Promise<R[]> {
131120
if (runInParallel) {
132-
return parallel(coll.map(mapper));
121+
return Promise.all(coll.map(mapper));
133122
}
134123

135124
const newColl: R[] = [];
@@ -152,7 +141,7 @@ export async function asyncfilter<T>(
152141
): Promise<T[]> {
153142
const newColl: T[] = [];
154143
if (runInParallel) {
155-
const bools = await parallel(coll.map(filter));
144+
const bools = await Promise.all(coll.map(filter));
156145
for (let i = 0; i < coll.length; i++) {
157146
if (bools[i]) {
158147
newColl.push(coll[i]);

test/asyncbox-specs.ts

Lines changed: 33 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
longSleep,
66
retry,
77
retryInterval,
8-
parallel,
98
asyncmap,
109
asyncfilter,
1110
waitForCondition,
@@ -173,88 +172,45 @@ describe('retry', function () {
173172
});
174173
});
175174

176-
describe('parallel', function () {
177-
const asyncFn = async function (val: number): Promise<number> {
178-
await sleep(50);
179-
return val;
180-
};
181-
const badAsyncFn = async function (): Promise<never> {
182-
await sleep(20);
183-
throw new Error('boo');
184-
};
185-
it('should perform tasks in parallel and return results', async function () {
186-
const vals = [1, 2, 3];
187-
const promises: Promise<number>[] = [];
188-
const start = Date.now();
189-
for (const v of vals) {
190-
promises.push(asyncFn(v));
175+
describe('waitForCondition', function () {
176+
let requestSpy: sinon.SinonSpy;
177+
beforeEach(function () {
178+
requestSpy = sinon.spy(B, 'delay');
179+
});
180+
afterEach(function () {
181+
requestSpy.restore();
182+
});
183+
it('should wait and succeed', async function () {
184+
const ref = Date.now();
185+
function condFn (): boolean {
186+
return Date.now() - ref > 200;
191187
}
192-
const res = await parallel(promises);
193-
expect(Date.now() - start).to.be.at.least(49);
194-
expect(Date.now() - start).to.be.below(59);
195-
expect(res.sort()).to.eql([1, 2, 3]);
188+
const result = await waitForCondition(condFn, {waitMs: 1000, intervalMs: 10});
189+
const duration = Date.now() - ref;
190+
expect(duration).to.be.above(200);
191+
expect(duration).to.be.below(250);
192+
expect(result).to.be.true;
196193
});
197-
it('should error with first response', async function () {
198-
const vals = [1, 2, 3];
199-
const promises: Promise<number>[] = [];
200-
const start = Date.now();
201-
for (const v of vals) {
202-
promises.push(asyncFn(v));
194+
it('should wait and fail', async function () {
195+
const ref = Date.now();
196+
function condFn (): boolean {
197+
return Date.now() - ref > 200;
203198
}
204-
promises.push(badAsyncFn());
205-
let err: Error | null = null;
206-
let res: number[] = [];
207199
try {
208-
res = await parallel(promises);
209-
} catch (e) {
210-
err = e as Error;
200+
await waitForCondition(condFn, {waitMs: 100, intervalMs: 10});
201+
expect.fail('Should have thrown an error');
202+
} catch (err: any) {
203+
expect(err.message).to.match(/Condition unmet/);
211204
}
212-
expect(Date.now() - start).to.be.at.least(19);
213-
expect(Date.now() - start).to.be.below(49);
214-
expect(err).to.exist;
215-
expect(res).to.eql([]);
216205
});
217-
218-
describe('waitForCondition', function () {
219-
let requestSpy: sinon.SinonSpy;
220-
beforeEach(function () {
221-
requestSpy = sinon.spy(B, 'delay');
222-
});
223-
afterEach(function () {
224-
requestSpy.restore();
225-
});
226-
it('should wait and succeed', async function () {
227-
const ref = Date.now();
228-
function condFn (): boolean {
229-
return Date.now() - ref > 200;
230-
}
231-
const result = await waitForCondition(condFn, {waitMs: 1000, intervalMs: 10});
232-
const duration = Date.now() - ref;
233-
expect(duration).to.be.above(200);
234-
expect(duration).to.be.below(250);
235-
expect(result).to.be.true;
236-
});
237-
it('should wait and fail', async function () {
238-
const ref = Date.now();
239-
function condFn (): boolean {
240-
return Date.now() - ref > 200;
241-
}
242-
try {
243-
await waitForCondition(condFn, {waitMs: 100, intervalMs: 10});
244-
expect.fail('Should have thrown an error');
245-
} catch (err: any) {
246-
expect(err.message).to.match(/Condition unmet/);
247-
}
248-
});
249-
it('should not exceed implicit wait timeout', async function () {
250-
const ref = Date.now();
251-
function condFn (): boolean {
252-
return Date.now() - ref > 15;
253-
}
254-
await (waitForCondition(condFn, {waitMs: 20, intervalMs: 10}));
255-
const getLastCall = requestSpy.getCall(1);
256-
expect(getLastCall.args[0]).to.be.at.most(10);
257-
});
206+
it('should not exceed implicit wait timeout', async function () {
207+
const ref = Date.now();
208+
function condFn (): boolean {
209+
return Date.now() - ref > 15;
210+
}
211+
await (waitForCondition(condFn, {waitMs: 20, intervalMs: 10}));
212+
const getLastCall = requestSpy.getCall(1);
213+
expect(getLastCall.args[0]).to.be.at.most(10);
258214
});
259215
});
260216

0 commit comments

Comments
 (0)