std/async/pool_test.ts
William Perron 69f3d67cb2
chore: remove Promise<void> return type annotation (#819)
There's a couple of instances where `Promise<void>` is still there,
those are the exceptions, for example functions that explicitely return
a `Promise` object or functions that return the promise from another
function call without any `await` statement.

used this one-liner to automatically remove everything:

```bash
grep -rli 'promise<void>' | xargs sed -i 's/: Promise<void>//g
```
2021-04-05 07:49:05 -04:00

45 lines
1.3 KiB
TypeScript

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { delay } from "./delay.ts";
import { pooledMap } from "./pool.ts";
import {
assert,
assertEquals,
assertStringIncludes,
assertThrowsAsync,
} from "../testing/asserts.ts";
Deno.test("[async] pooledMap", async function () {
const start = new Date();
const results = pooledMap(
2,
[1, 2, 3],
(i) => new Promise((r) => setTimeout(() => r(i), 1000)),
);
for await (const value of results) {
console.log(value);
}
const diff = new Date().getTime() - start.getTime();
assert(diff >= 2000);
assert(diff < 3000);
});
Deno.test("[async] pooledMap errors", async function () {
async function mapNumber(n: number): Promise<number> {
if (n <= 2) {
throw new Error(`Bad number: ${n}`);
}
await delay(100);
return n;
}
const mappedNumbers: number[] = [];
const error = await assertThrowsAsync(async () => {
for await (const m of pooledMap(3, [1, 2, 3, 4], mapNumber)) {
mappedNumbers.push(m);
}
}, AggregateError) as AggregateError;
assertEquals(mappedNumbers, [3]);
assertEquals(error.errors.length, 2);
assertStringIncludes(error.errors[0].stack, "Error: Bad number: 1");
assertStringIncludes(error.errors[1].stack, "Error: Bad number: 2");
});