@ts-delight/async-to-generator.macro
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

About

async-to-generator is a babel macro to transform async functions to generator functions.

The intent is primarily to facilitate type-safety in libraries (like mobx-state-tree) where generators are used (for being interceptable) with same semantics as async/await.

Example

Source:

import a2g from '@ts-delight/async-to-generator.macro';
import {flow} from 'mobx-state-tree';

flow(a2g(async () => {
  const result = await fetch("/data");
}));

Compiled output:

import {flow} from 'mobx-state-tree';

flow(function* () {
  const result = yield fetch("/data");
});

Installation

This utility is implemented as a babel-macro.

Refer babel's setup instructions to learn how to setup your project to use babel for compilation. Usage with tsc (only) is not supported.

  1. Install babel-plugin-macros and async-to-generator.macro:
npm install --save-dev babel-plugin-macros @ts-delight/async-to-generator.macro
  1. Add babel-plugin-macros to .babelrc (if not already preset):
// .babelrc

module.exports = {
  presets: [
    '@babel/preset-typescript',
    // ... other presets
  ],
  plugins: [
    'babel-plugin-macros',
    // ... other plugins
  ],
};

License

MIT

Package Sidebar

Install

npm i @ts-delight/async-to-generator.macro

Weekly Downloads

2

Version

1.0.2

License

MIT

Unpacked Size

6.67 kB

Total Files

12

Last publish

Collaborators

  • lorefnon