Skip to content

Commit

Permalink
initial steps to prefer-await-over-promises
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Dec 9, 2022
1 parent b71b510 commit 369649c
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ rules:
promise/no-return-wrap: error
promise/param-names: off
promise/prefer-await-to-callbacks: off
promise/prefer-await-to-then: off
promise/prefer-await-to-then: error
promise/valid-params: error

##############################################################################
Expand Down
13 changes: 7 additions & 6 deletions src/__testUtils__/__tests__/resolveOnNextTick-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ describe('resolveOnNextTick', () => {
it('resolves promise on the next tick', async () => {
const output = [];

const promise1 = resolveOnNextTick().then(() => {
output.push('second');
});
const promise2 = resolveOnNextTick().then(() => {
output.push('third');
});
async function outputOnNextTick(message: string) {
await resolveOnNextTick();
output.push(message);
}

const promise1 = outputOnNextTick('second');
const promise2 = outputOnNextTick('third');
output.push('first');

await Promise.all([promise1, promise2]);
Expand Down
7 changes: 6 additions & 1 deletion src/__testUtils__/expectEqualPromisesOrValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';

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

async function expectMatchingPromises<T>(items: Promise<ReadonlyArray<T>>) {
const values = await items;
return expectMatchingValues(values);
}

export function expectEqualPromisesOrValues<T>(
items: ReadonlyArray<PromiseOrValue<T>>,
): PromiseOrValue<T> {
const [firstItem, ...remainingItems] = items;
if (isPromise(firstItem)) {
if (remainingItems.every(isPromise)) {
return Promise.all(items).then(expectMatchingValues);
return expectMatchingPromises(Promise.all(items));
}
} else if (remainingItems.every((item) => !isPromise(item))) {
return expectMatchingValues(items);
Expand Down
10 changes: 7 additions & 3 deletions src/execution/__tests__/simplePubSub-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ describe('SimplePubSub', () => {
value: 'Banana',
});

async function getNextItem(i: AsyncIterator<unknown>) {
return i.next();
}

// Read ahead
const i3 = iterator.next().then((x) => x);
const i4 = iterator.next().then((x) => x);
const i3 = getNextItem(iterator);
const i4 = getNextItem(iterator);

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

// Read ahead
const i5 = iterator.next().then((x) => x);
const i5 = getNextItem(iterator);

// Terminate queue
await iterator.return();
Expand Down
Loading

0 comments on commit 369649c

Please sign in to comment.