Skip to content

Commit d5b10d7

Browse files
committed
change @typescript-eslint/return-await to 'always'
Motivation: It is faster to await a promise prior to returning it from an async function than to return a promise with `.then()`. See: https://github.com/tc39/proposal-faster-promise-adoption Previously, this eslint rule errored by default only 'in-try-catch'.
1 parent 5e536cc commit d5b10d7

File tree

7 files changed

+19
-17
lines changed

7 files changed

+19
-17
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ overrides:
651651
]
652652
'@typescript-eslint/no-useless-constructor': error
653653
'@typescript-eslint/require-await': error
654-
'@typescript-eslint/return-await': error
654+
'@typescript-eslint/return-await': [error, 'always']
655655

656656
# Disable for JS and TS
657657
'@typescript-eslint/init-declarations': off

resources/gen-changelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ async function getPRsInfo(
294294
let prNumbers = await splitBatches(commits, batchCommitToPR);
295295
prNumbers = Array.from(new Set(prNumbers)); // Remove duplicates
296296

297-
return splitBatches(prNumbers, batchPRInfo);
297+
return await splitBatches(prNumbers, batchPRInfo);
298298
}
299299

300300
// Split commits into batches of 50 to prevent timeouts

src/execution/__tests__/simplePubSub-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('SimplePubSub', () => {
2323
});
2424

2525
async function getNextItem(i: AsyncIterator<unknown>) {
26-
return i.next();
26+
return await i.next();
2727
}
2828

2929
// Read ahead

src/execution/__tests__/stream-test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ async function completeAsync(
126126
for (let i = 0; i < numCalls; i++) {
127127
promises.push(iterator.next());
128128
}
129-
return Promise.all(promises);
129+
return await Promise.all(promises);
130130
}
131131

132132
function createResolvablePromise<T>(): [Promise<T>, (value?: T) => void] {
@@ -531,11 +531,6 @@ describe('Execute: stream directive', () => {
531531
},
532532
],
533533
},
534-
],
535-
hasNext: true,
536-
},
537-
{
538-
incremental: [
539534
{
540535
items: [{ name: 'Leia', id: '3' }],
541536
path: ['friendList', 2],

src/execution/execute.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,9 @@ async function completeAsyncIteratorValue(
10771077
}
10781078
index += 1;
10791079
}
1080-
return containsPromise ? Promise.all(completedResults) : completedResults;
1080+
return containsPromise
1081+
? await Promise.all(completedResults)
1082+
: completedResults;
10811083
}
10821084

10831085
/**
@@ -2222,7 +2224,7 @@ function yieldSubsequentPayloads(
22222224
const hasNext = exeContext.subsequentPayloads.size > 0;
22232225

22242226
if (!incremental.length && hasNext) {
2225-
return next();
2227+
return await next();
22262228
}
22272229

22282230
if (!hasNext) {
@@ -2265,7 +2267,7 @@ function yieldSubsequentPayloads(
22652267
): Promise<IteratorResult<SubsequentIncrementalExecutionResult, void>> {
22662268
await returnStreamIterators();
22672269
isDone = true;
2268-
return Promise.reject(error);
2270+
return await Promise.reject(error);
22692271
},
22702272
};
22712273
}

src/execution/mapAsyncIterable.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ export function mapAsyncIterable<T, U, R = undefined>(
3636

3737
return {
3838
async next() {
39-
return mapResult(await iterator.next());
39+
return await mapResult(await iterator.next());
4040
},
4141
async return(): Promise<IteratorResult<U, R>> {
4242
// If iterator.return() does not exist, then type R must be undefined.
4343
return typeof iterator.return === 'function'
44-
? mapResult(await iterator.return())
44+
? await mapResult(await iterator.return())
4545
: { value: undefined as any, done: true };
4646
},
4747
async throw(error?: unknown) {
4848
if (typeof iterator.throw === 'function') {
49-
return mapResult(await iterator.throw(error));
49+
return await mapResult(await iterator.throw(error));
5050
}
5151
throw error;
5252
},

src/jsutils/after.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import type { PromiseOrValue } from './PromiseOrValue';
1+
import { isPromise } from './isPromise.js';
2+
import type { PromiseOrValue } from './PromiseOrValue.js';
23

34
export async function after<T, R>(
45
promise: Promise<T>,
56
onFulfilled: (value: T) => PromiseOrValue<R>,
67
): Promise<R> {
7-
return onFulfilled(await promise);
8+
const result = onFulfilled(await promise);
9+
if (isPromise(result)) {
10+
return await result;
11+
}
12+
return result;
813
}

0 commit comments

Comments
 (0)