Skip to content

Commit 6b2b467

Browse files
committed
enable promise/prefer-await-to-then
leads to one less tick in `executeStreamField` in the branch where completion of a non-promise yields a promise.
1 parent eea0e14 commit 6b2b467

File tree

7 files changed

+212
-138
lines changed

7 files changed

+212
-138
lines changed

.eslintrc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ rules:
196196
promise/no-return-wrap: error
197197
promise/param-names: off
198198
promise/prefer-await-to-callbacks: off
199-
promise/prefer-await-to-then: off
199+
promise/prefer-await-to-then: error
200200
promise/valid-params: error
201201

202202
##############################################################################

src/__testUtils__/__tests__/resolveOnNextTick-test.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ describe('resolveOnNextTick', () => {
77
it('resolves promise on the next tick', async () => {
88
const output = [];
99

10-
const promise1 = resolveOnNextTick().then(() => {
11-
output.push('second');
12-
});
13-
const promise2 = resolveOnNextTick().then(() => {
14-
output.push('third');
15-
});
10+
async function outputOnNextTick(message: string) {
11+
await resolveOnNextTick();
12+
output.push(message);
13+
}
14+
15+
const promise1 = outputOnNextTick('second');
16+
const promise2 = outputOnNextTick('third');
1617
output.push('first');
1718

1819
await Promise.all([promise1, promise2]);

src/__testUtils__/expectEqualPromisesOrValues.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';
55

66
import { expectMatchingValues } from './expectMatchingValues.js';
77

8+
async function expectMatchingPromises<T>(items: Promise<ReadonlyArray<T>>) {
9+
const values = await items;
10+
return expectMatchingValues(values);
11+
}
12+
813
export function expectEqualPromisesOrValues<T>(
914
items: ReadonlyArray<PromiseOrValue<T>>,
1015
): PromiseOrValue<T> {
1116
const [firstItem, ...remainingItems] = items;
1217
if (isPromise(firstItem)) {
1318
if (remainingItems.every(isPromise)) {
14-
return Promise.all(items).then(expectMatchingValues);
19+
return expectMatchingPromises(Promise.all(items));
1520
}
1621
} else if (remainingItems.every((item) => !isPromise(item))) {
1722
return expectMatchingValues(items);

src/execution/__tests__/simplePubSub-test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ describe('SimplePubSub', () => {
2222
value: 'Banana',
2323
});
2424

25+
async function getNextItem(i: AsyncIterator<unknown>) {
26+
return i.next();
27+
}
28+
2529
// Read ahead
26-
const i3 = iterator.next().then((x) => x);
27-
const i4 = iterator.next().then((x) => x);
30+
const i3 = getNextItem(iterator);
31+
const i4 = getNextItem(iterator);
2832

2933
// Publish
3034
expect(pubsub.emit('Coconut')).to.equal(true);
@@ -35,7 +39,7 @@ describe('SimplePubSub', () => {
3539
expect(await i3).to.deep.equal({ done: false, value: 'Coconut' });
3640

3741
// Read ahead
38-
const i5 = iterator.next().then((x) => x);
42+
const i5 = getNextItem(iterator);
3943

4044
// Terminate queue
4145
await iterator.return();

src/execution/__tests__/stream-test.ts

-5
Original file line numberDiff line numberDiff line change
@@ -984,11 +984,6 @@ describe('Execute: stream directive', () => {
984984
},
985985
],
986986
},
987-
],
988-
hasNext: true,
989-
},
990-
{
991-
incremental: [
992987
{
993988
items: [{ nonNullName: 'Han' }],
994989
path: ['friendList', 2],

0 commit comments

Comments
 (0)