JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 27
  • Score
    100M100P100Q59521F
  • License MIT

transforms promise chains into async/await

Package Exports

  • @codemodsquad/asyncify

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@codemodsquad/asyncify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@codemodsquad/asyncify

CircleCI Coverage Status semantic-release Commitizen friendly npm version

Transforms promise chains into async/await. I wrote this to refactor the 5000+ .then/.catch/.finally calls in the sequelize codebase. This is slightly inspired by async-await-codemod, but written from scratch to guarantee the same behavior and tidy code as best as I can manage.

Usage

git clone https://github.com/codemodsquad/asyncify
npx jscodeshift -t asyncify/index.js path/to/your/project/**/*.js

Support table

asyncify
Converts .then
Converts .catch
Converts .finally
Renames identifiers in handlers that would conflict
Converts promise chains that aren't returned/awaited into IIAAFs
Converts return Promise.resolve()/return Promise.reject()
Removes unnecessary Promise.resolve() wrappers
Warns when the original function could return/throw a non-promise Planned
Refactoring/inlining handlers that contain conditional returns
All but one if/else/switch branch return
All branches return, even nested ones
All but one nested if/else/switch branch return 🚫
More than one if/else/switch branch doesn't return 🚫
Return inside loop 🚫

Warnings

Comments can sometimes get deleted due to an impedance mismatch between @babel and recast ASTs. If you use the --commentWorkarounds=true option it will try to prevent more comments from getting deleted but it sometimes causes an assertion to fail in recast.

There are a few edge cases where asyncify produces funky output. It's intended to not break any existing behavior (at the moment there are a few bugs that do, which I need to fix) but sometimes the output will be be semantically wrong even if it behaves correctly. For example, I've seen a case where doing an async operation several times in a row:

it('test', () => {
  const doSomething = () => {
    // ...
  }

  return doSomething()
    .then(doSomething)
    .then(doSomething)
}

Gets converted to:

it('test', async () => {
  const doSomething = () => {
    // ...
  }
  await doSomething(await doSomething(await doSomething()))
})

This works even though it initially seems like it wouldn't and is obviously not what you want:

it('test', async () => {
  const doSomething = () => {
    // ...
  }
  await doSomething()
  await doSomething()
  await doSomething()
})

Although I could possibly fix this for cases where it's easy to determine that the function has no parameters, there could be cases where it's impossible to determine whether the identifier doSomething is even a function or whether it has parameters.