# promisify-generator

> Convert a Generator into Promise~!

Latest version **1.0.0** (published 2018-01-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install promisify-generator
pnpm add promisify-generator
yarn add promisify-generator
bun add promisify-generator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-01-07 |
| First published | 2018-01-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Luke Edwards |
| Maintainers | lukeed |
| Keywords | generator, generator-function, promisify, promise |

## Links

- npm: https://www.npmjs.com/package/promisify-generator
- Repository: https://github.com/lukeed/promisify-generator
- Homepage: https://github.com/lukeed/promisify-generator#readme
- Issues: https://github.com/lukeed/promisify-generator/issues
- npm.io page: https://npm.io/package/promisify-generator

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-01-07

## README

# promisify-generator [![Build Status](https://travis-ci.org/lukeed/promisify-generator.svg?branch=master)](https://travis-ci.org/lukeed/promisify-generator)

> Convert a Generator into Promise~!

Lightweight utility to promisify a generator, without the need for an entire library like [`bluebird`](https://github.com/petkaantonov/bluebird) or [`co`](https://github.com/tj/co).


## Install

```sh
$ npm install --save promisify-generator
```


## Usage

```js
const pgen = require('promisify-generator');

function * foo() {
  let idx = 0;
  while (idx <= 5) {
    yield idx++;
  }
  return idx;
}

function * bar() {
  let idx = 0;
  while (idx <= 5) {
    if (idx === 3) {
      throw new Error('EQUALS THREE');
    }
    yield idx++;
  }
  return idx;
}

const pFoo = pgen(foo);
//=> [Function]
pFoo().then(console.log);
//=> 6

const pBar = pgen(bar);
pBar().then().catch(console.error);
//=> Error: EQUALS THREE
```


## API

### pgen(fn)

#### fn

Type: `GeneratorFunction`<br>
Returns: `Function`

The [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator) function to promisify.

A normal `Function` is returned, which invokes a `Promise` when called. Any arguments passed to this function will be passed to your orginal Generator.

> **Important:** This library **does not** validate that your `fn` is, in fact, a `GeneratorFunction`!

To manually check if `fn` is a Generator, you may use the [`is-generator-function`](https://www.npmjs.com/package/is-generator-function) module, or the following snippet:

```js
const isGenerator = fn => fn.constructor.name === 'GeneratorFunction';
```


## License

MIT © [Luke Edwards](https://lukeed.com)

---
_Source: https://npm.io/package/promisify-generator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
