Skip to content

Commit 4106691

Browse files
authored
fix(core): update getLastValueFromAsyncIterableIterator to support AsyncIterables returned from executors (#23229)
When an executor returns an `AsyncIterable` Nx fails because it cannot read the value using `getLastValueFromAsyncIterableIterator` (which only supports `AsyncIterableIterator`. This PR updates it to support both so executors like `@nx/rollup:rollup` will work. <!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior Running Nx command fails if executor returns `AsyncIterable` e.g. by calling `createAsyncIterable`. ## Expected Behavior Nx command succeeds when executor returns `AsyncIterable`. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #23028
1 parent 078dd06 commit 4106691

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

packages/devkit/src/utils/async-iterable/create-async-iterable.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createAsyncIterable } from './create-async-iterable';
2+
import { getLastValueFromAsyncIterableIterator } from 'nx/src/utils/async-iterator';
23

34
describe(createAsyncIterable.name, () => {
45
test('simple callback', async () => {
@@ -55,4 +56,15 @@ describe(createAsyncIterable.name, () => {
5556

5657
expect(results).toEqual(['first', 'second', 'third', 'fourth']);
5758
});
59+
60+
test('works with getLastValueFromAsyncIterableIterator', async () => {
61+
const it = createAsyncIterable<string>(({ next, done }) => {
62+
setTimeout(() => {
63+
next('foo');
64+
done();
65+
});
66+
});
67+
const result = await getLastValueFromAsyncIterableIterator(it);
68+
expect(result).toEqual('foo');
69+
});
5870
});

packages/nx/src/utils/async-iterator.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ export function isAsyncIterator<T>(v: any): v is AsyncIterableIterator<T> {
33
}
44

55
export async function getLastValueFromAsyncIterableIterator<T>(
6-
i: AsyncIterableIterator<T>
6+
i: AsyncIterable<T> | AsyncIterableIterator<T>
77
): Promise<T> {
88
let prev: IteratorResult<T, T>;
99
let current: IteratorResult<T, T>;
10+
11+
const generator = i[Symbol.asyncIterator] || i[Symbol.iterator];
12+
const iterator = generator.call(i);
13+
1014
do {
1115
prev = current;
12-
current = await i.next();
16+
current = await iterator.next();
1317
} while (!current.done);
1418

1519
return current.value !== undefined || !prev ? current.value : prev.value;

0 commit comments

Comments
 (0)