Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove eachAsync
  • Loading branch information
W-A-James committed Mar 8, 2024
commit b9ee91c03f4114b538d4a70b8dbe46cd9823c5a3
40 changes: 0 additions & 40 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,46 +386,6 @@ export function maxWireVersion(topologyOrServer?: Connection | Topology | Server
return 0;
}

/**
* Applies the function `eachFn` to each item in `arr`, in parallel.
* @internal
*
* @param arr - An array of items to asynchronously iterate over
* @param eachFn - A function to call on each item of the array. The callback signature is `(item, callback)`, where the callback indicates iteration is complete.
* @param callback - The callback called after every item has been iterated
*/
export function eachAsync<T = Document>(
arr: T[],
eachFn: (item: T, callback: (err?: AnyError) => void) => void,
callback: Callback
): void {
arr = arr || [];

let idx = 0;
let awaiting = 0;
for (idx = 0; idx < arr.length; ++idx) {
awaiting++;
eachFn(arr[idx], eachCallback);
}

if (awaiting === 0) {
callback();
return;
}

function eachCallback(err?: AnyError) {
awaiting--;
if (err) {
callback(err);
return;
}

if (idx === arr.length && awaiting <= 0) {
callback();
}
}
}

/** @internal */
export function arrayStrictEqual(arr: unknown[], arr2: unknown[]): boolean {
if (!Array.isArray(arr) || !Array.isArray(arr2)) {
Expand Down
32 changes: 0 additions & 32 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
BufferPool,
ByteUtils,
compareObjectId,
eachAsync,
HostAddress,
hostMatchesWildcards,
isHello,
Expand Down Expand Up @@ -164,37 +163,6 @@ describe('driver utils', function () {
});
});

context('eachAsync()', function () {
it('should callback with an error', function (done) {
eachAsync(
[{ error: false }, { error: true }],
(item, cb) => {
cb(item.error ? new Error('error requested') : undefined);
},
err => {
expect(err).to.exist;
done();
}
);
});

it('should propagate a synchronously thrown error', function (done) {
expect(() =>
eachAsync(
[{}],
() => {
throw new Error('something wicked');
},
err => {
expect(err).to.not.exist;
done(err);
}
)
).to.throw(/something wicked/);
done();
});
});

describe('class BufferPool', function () {
it('should report the correct length', function () {
const buffer = new BufferPool();
Expand Down