I'm new to async-wait programming in typescript, so I wrote an experiment:
import { AssertionError } from 'assert'
async function handle(numbers: Array<number>) {
numbers.forEach(compute)
}
async function compute(n: number) {
let primesFound = 0;
for (let p = 2; primesFound < n; p++) {
if (isPrime(p)) {
console.log(`found prime: ${p}`)
primesFound++;
}
}
throw new AssertionError();
}
function isPrime(p: number) {
for (let i=2;i<p;i++) {
for (let j=2;j<p;j++) {
if (i*j === p) {
return false;
}
}
}
return true;
}
async function main() {
await handle([1000, 1000, 5])
}
main()
I expected the three computations to happen in parallel, with the shortest of them hitting the assertion failure first - that did not happen. The three computations happened sequentially, and only after they all finished the assertion fired:
... // omitted for brevity
found prime: 7907
found prime: 7919
found prime: 2
found prime: 3
found prime: 5
found prime: 7
found prime: 11
node:internal/errors:484
ErrorCaptureStackTrace(err);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "options" argument must be of type object. Received undefined
at new AssertionError (node:internal/assert/assertion_error:327:5)