Skip to content

Commit

Permalink
Fix #21
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-van committed Oct 6, 2024
1 parent c99090a commit e3fff42
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/asyncGeneratorMap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,17 @@ async function * asyncGeneratorMap (iterable, iteratee, queueOrConcurrency = 1,
if (result.snapshot.status === 'rejected') {
throw result.snapshot.reason
} else {
yield result.snapshot.value
let yielded = false
try {
yield result.snapshot.value
yielded = true
} finally {
if (!yielded)
{

Check failure on line 192 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Opening curly brace does not appear on the same line as controlling statement

Check failure on line 192 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Opening curly brace does not appear on the same line as controlling statement

Check failure on line 192 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Opening curly brace does not appear on the same line as controlling statement

Check failure on line 192 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Opening curly brace does not appear on the same line as controlling statement

Check failure on line 192 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Opening curly brace does not appear on the same line as controlling statement

Check failure on line 192 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Opening curly brace does not appear on the same line as controlling statement

Check failure on line 192 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Opening curly brace does not appear on the same line as controlling statement
await it.return()
return

Check failure on line 194 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Unsafe usage of ReturnStatement

Check failure on line 194 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Unsafe usage of ReturnStatement

Check failure on line 194 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Unsafe usage of ReturnStatement

Check failure on line 194 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Unsafe usage of ReturnStatement

Check failure on line 194 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Unsafe usage of ReturnStatement

Check failure on line 194 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Unsafe usage of ReturnStatement

Check failure on line 194 in src/asyncGeneratorMap.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Unsafe usage of ReturnStatement
}
}
}
}
if (exhausted && lastIndexFetched === lastIndexHandled) {
Expand Down
72 changes: 72 additions & 0 deletions src/asyncGeneratorMap.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,75 @@ test('asyncGeneratorMap reaches concurrency', async () => {
expect(maxConcurrency).toStrictEqual(expectedConcurrency)
expect(result).toStrictEqual(await asyncIterableToArray(range(100)))
})

test('common for await interrupts generator on interrupted iteration', async () => {

Check failure on line 636 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Block must not be padded by blank lines

Check failure on line 636 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Block must not be padded by blank lines

Check failure on line 636 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Block must not be padded by blank lines

Check failure on line 636 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Block must not be padded by blank lines

Check failure on line 636 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Block must not be padded by blank lines

Check failure on line 636 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Block must not be padded by blank lines

Check failure on line 636 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Block must not be padded by blank lines

let finallyReached = false;

Check failure on line 638 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Extra semicolon

Check failure on line 638 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Extra semicolon

Check failure on line 638 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Extra semicolon

Check failure on line 638 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Extra semicolon

Check failure on line 638 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Extra semicolon

Check failure on line 638 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Extra semicolon

Check failure on line 638 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Extra semicolon

async function *asyncGenWithFinally() {

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Missing JSDoc comment

Check warning on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Missing JSDoc comment

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Missing space after *

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Missing space before function parentheses

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing JSDoc comment

Check warning on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing JSDoc comment

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing space after *

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing space before function parentheses

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Missing JSDoc comment

Check warning on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Missing JSDoc comment

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Missing space after *

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Missing space before function parentheses

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Missing JSDoc comment

Check warning on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Missing JSDoc comment

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Missing space after *

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Missing space before function parentheses

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Missing JSDoc comment

Check warning on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Missing JSDoc comment

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Missing space after *

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Missing space before function parentheses

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing JSDoc comment

Check warning on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing JSDoc comment

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing space after *

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing space before function parentheses

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Missing JSDoc comment

Check warning on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Missing JSDoc comment

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Missing space after *

Check failure on line 640 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Missing space before function parentheses
try {
for (const element of range(10)) {
yield element;

Check failure on line 643 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Extra semicolon

Check failure on line 643 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Extra semicolon

Check failure on line 643 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Extra semicolon

Check failure on line 643 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Extra semicolon

Check failure on line 643 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Extra semicolon

Check failure on line 643 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Extra semicolon

Check failure on line 643 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Extra semicolon
}
} finally {
finallyReached = true;

Check failure on line 646 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Extra semicolon

Check failure on line 646 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Extra semicolon

Check failure on line 646 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Extra semicolon

Check failure on line 646 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Extra semicolon

Check failure on line 646 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Extra semicolon

Check failure on line 646 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Extra semicolon

Check failure on line 646 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Extra semicolon
}
}

for await (const n of asyncGenWithFinally()) {
if (n === 2) {
break
}
}

expect(finallyReached).toBe(true);

Check failure on line 656 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Extra semicolon

Check failure on line 656 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Extra semicolon

Check failure on line 656 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Extra semicolon

Check failure on line 656 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Extra semicolon

Check failure on line 656 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Extra semicolon

Check failure on line 656 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Extra semicolon

Check failure on line 656 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Extra semicolon
})

test('common for await calls AsyncGenerator.return() not AsyncGenerator.throw()', async () => {

let catchedException = null;

async function *asyncGenWithFinally() {

Check warning on line 663 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Missing JSDoc comment

Check warning on line 663 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing JSDoc comment

Check warning on line 663 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Missing JSDoc comment

Check warning on line 663 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Missing JSDoc comment

Check warning on line 663 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Missing JSDoc comment

Check warning on line 663 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing JSDoc comment

Check warning on line 663 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Missing JSDoc comment
try {
for (const element of range(10)) {
yield element;
}
} catch (e) {
catchedException = e;
}
}

try {
for await (const n of asyncGenWithFinally()) {
if (n === 2) {
throw new TestError();
}
}
} catch (e) // ignore
{
expect(e instanceof TestError).toBe(true)
}
expect(catchedException).toBe(null);
})

test('asyncGeneratorMap interrupts generator on interrupted iteration', async () => {

let finallyReached = false;

async function *asyncGenWithFinally() {

Check warning on line 690 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (12.x)

Missing JSDoc comment

Check warning on line 690 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing JSDoc comment

Check warning on line 690 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (14.x)

Missing JSDoc comment

Check warning on line 690 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (16.x)

Missing JSDoc comment

Check warning on line 690 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (21.x)

Missing JSDoc comment

Check warning on line 690 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing JSDoc comment

Check warning on line 690 in src/asyncGeneratorMap.test.mjs

View workflow job for this annotation

GitHub Actions / test (22.x)

Missing JSDoc comment
try {
for (const element of range(10)) {
yield element;
}
} finally {
finallyReached = true;
}
}

for await (const n of asyncGeneratorMap(asyncGenWithFinally(), n => n*2)) {
if (n === 4) {
break
}
}
expect(finallyReached).toBe(true);
})

0 comments on commit e3fff42

Please sign in to comment.