# esqlate-promise-returning-function-to-generator

> Converts a function that returns a Promise, along with a check to decide whether we should continue into a `for-of` generator.

Latest version **1.0.1** (published 2021-12-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install esqlate-promise-returning-function-to-generator
pnpm add esqlate-promise-returning-function-to-generator
yarn add esqlate-promise-returning-function-to-generator
bun add esqlate-promise-returning-function-to-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.1 |
| Published | 2021-12-01 |
| First published | 2019-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matt Forrester |
| Maintainers | forbesmyster |
| Keywords | promise, generator, async |

## Links

- npm: https://www.npmjs.com/package/esqlate-promise-returning-function-to-generator
- npm.io page: https://npm.io/package/esqlate-promise-returning-function-to-generator

## Dependencies (3)

- [ava](https://npm.io/package/ava.md) ^3.15.0
- [ts-node](https://npm.io/package/ts-node.md) ^8.4.1
- [typescript](https://npm.io/package/typescript.md) ^3.6.4

## Alternatives

- [@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
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-12-01
- 1.0.0 — 2019-11-08

## README

# Esqlate Promise Returning Function To Generator

Converts a function that returns a Promise, along with a check to decide whether we should continue into a `for-of` generator.


```javascript
type DBLine = [string, string, number];
interface Result<R> {
    command: string;
    fields: { name: string }[],
    rows: R[]
}

function getQueryArrayResults(r: DBLine[]): Result<DBLine> {
    return {
        command: 'SELECT',
        fields: [
            { name: "first_name" },
            { name: "last_name" },
            { name: "age" }
        ],
        rows: r
    };
}

let rows: Result<DBLine>[] = [
    getQueryArrayResults(
        [["matt", "forrester", 39], ["james", "jon", 44], ["bill", "bob", 33]]
    ),
    getQueryArrayResults(
        [["susan", "smith", 39], ["sara", "james", 44], ["boris", "black", 33]]
    ),
    getQueryArrayResults(
        [["alice", "james", 39], ["alex", "halt", 44], ["carl", "blue", 33]]
    ),
];

let cursorIndex: number = 0;

function getter(): Promise<Result<DBLine>> {
    if (cursorIndex++ >= rows.length) {
        return Promise.resolve(getQueryArrayResults([]));
    }
    if (cursorIndex == 2) {
        throw new Error("Error getting result");
    }
    return Promise.resolve(rows[cursorIndex - 1]);
}

function isComplete(r: Result<DBLine>) {
    return r.rows.length === 0;
}

let r: Result<DBLine>[] = [];
try {
    for await (const qar of streamUntil(getter, isComplete)()) {
        r.push(qar);
    }
} catch (e) {
    assert.deepEqual(r, [rows[0]]);
}
```

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