solana-web3.js icon indicating copy to clipboard operation
solana-web3.js copied to clipboard

Enable `require-await` linter

Open steveluscher opened this issue 3 months ago • 3 comments

Summary

Alright, so this bad:

async function foo() {
  return somePromise;
}

This good:

async function foo() {
  return await somePromise;
}

To understand why, read this code carefully and test it for yourself.

async function test(name, fn) {
  let tick = 0;
  const tock = () => tick++;
  Promise.resolve().then(tock).then(tock).then(tock);

  const p = await fn();
  console.assert(p === 42);
  console.log(name, tick);
}

await Promise.all([
  test("nonpromise-sync", () => 42),
  test("nonpromise-async", async () => 42),
  test("nonpromise-async-await", async () => await 42),
  test("promise-sync", () => Promise.resolve(42)),
  test("promise-async", async () => Promise.resolve(42)),
  test("promise-async-await", async () => await Promise.resolve(42)),
  test("typical-usage-1", async () =>
    await (async () => await Promise.resolve(42))()),
  test("typical-usage-2", async () => await (() => Promise.resolve(42))()),
  test("typical-usage-3", async () =>
    await (async () => Promise.resolve(42))()),
]);

setTimeout(() => {}, 100);

Much more on this, here: https://stackoverflow.com/questions/43353087/are-there-performance-concerns-with-return-await/70979225#70979225

[!NOTE] This is the first ESLint rule that requires TypeScript to actually participate (it uses type information to do the linting). This will make ESLint way slower. Compare this CI run to the previous PR in this stack. This can also be annoying because lint may show false errors if you have not yet built types for your dependencies.

Test Plan

Run pnpm style:fix until there are no more lint errors.

steveluscher avatar Mar 21 '24 04:03 steveluscher