# promise-faker

> Provides promise-like APIs but runs synchronously. This module is useful for controlling flows.

Latest version **1.2.5** (published 2018-02-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-faker
pnpm add promise-faker
yarn add promise-faker
bun add promise-faker
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.5 |
| Published | 2018-02-22 |
| First published | 2018-01-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=4 |
| Dependencies | 2 |
| Unpacked size | 11.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | kaelzhang |
| Maintainers | kael |
| Keywords | promise-faker, fake-promise, sync, sync-promise, promise-sync, promise.sync, promise, promise.resolve, promise.reject, promise.all, fake, sync, async |

## Links

- npm: https://www.npmjs.com/package/promise-faker
- Repository: https://github.com/kaelzhang/promise-faker
- Homepage: https://github.com/kaelzhang/promise-faker#readme
- Issues: https://github.com/kaelzhang/promise-faker/issues
- npm.io page: https://npm.io/package/promise-faker

## Dependencies (2)

- [symbol-for](https://npm.io/package/symbol-for.md) ^1.0.1
- [graceful-instanceof](https://npm.io/package/graceful-instanceof.md) ^1.0.1

## 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.2.5 (latest) — 2018-02-22
- 1.2.4 — 2018-01-25
- 1.2.3 — 2018-01-25
- 1.2.2 — 2018-01-17
- 1.2.1 — 2018-01-17
- 1.2.0 — 2018-01-17
- 1.1.0 — 2018-01-17
- 1.0.3 — 2018-01-10
- 1.0.2 — 2018-01-10
- 1.0.1 — 2018-01-10
- 1.0.0 — 2018-01-10
- 0.0.0 — 2018-01-09

## README

[![Build Status](https://travis-ci.org/kaelzhang/promise-faker.svg?branch=master)](https://travis-ci.org/kaelzhang/promise-faker)
[![Coverage](https://codecov.io/gh/kaelzhang/promise-faker/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/promise-faker)
<!-- optional appveyor tst
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/github/kaelzhang/promise-faker?branch=master&svg=true)](https://ci.appveyor.com/project/kaelzhang/promise-faker)
-->
<!-- optional npm version
[![NPM version](https://badge.fury.io/js/promise-faker.svg)](http://badge.fury.io/js/promise-faker)
-->
<!-- optional npm downloads
[![npm module downloads per month](http://img.shields.io/npm/dm/promise-faker.svg)](https://www.npmjs.org/package/promise-faker)
-->
<!-- optional dependency status
[![Dependency Status](https://david-dm.org/kaelzhang/promise-faker.svg)](https://david-dm.org/kaelzhang/promise-faker)
-->

# promise-faker

Provides `Promise`-like APIs but runs synchronously. This module is useful for controlling flows.

## Install

```sh
$ npm install promise-faker
```

## Usage

```js
import FakePromise from 'promise-faker'

// Write flows as normal Promise does
function factory (p) {
  const result = p.resolve(1)
  .then(() => {
    return 2
  })

  // Not to make the following chain.
  return p.resolve(result, true)
}

// Then, run them as synchronous flows
factory(FakePromise)  // 2
factory(Promise)      // Promise {2}
```

FakePromise actually runs synchronously:

```js
Promise.resolve(1).then(console.log)
console.log(2)
// 2
// 1

FakePromise.resolve(3).then(console.log)
console.log(4)
// 3
// 4
```

## new FakePromise(executor)

- **executor** `Function(resolve, reject)`

Returns a fake promise

### FakePromise.resolve(subject [, end])

- **end** `?boolean=false` The additional parameter only for `FakePromise`, and if this parameter is `true`, it will try to get the final value or throw an error if there is a rejection.

```js
FakePromise.resolve(FakePromise.resolve(1), true)
// 1

FakePromise.resolve(FakePromise.reject('2'), true)
// -> throw '2'
```

And if the fake promise is still pending, an `Error('pending unexpectedly')` error will thrown.

```js
const p = new FakePromise((resolve, reject) => {
  return 1
})

try {
  FakePromise.resolve(p, true)
} catch (e) {
  console.log(e.message)  // 'pending unexpectedly'
}
```

### FakePromise.reject(subject)

Similar as `Promise.reject`, but returns a fake promise

### FakePromise.all(tasks)

Similar as `Promise.all`, but returns a fake promise

### promise.then(onResolve [, onReject])

Similar as `promise.then`, but returns a fake promise

### promise.catch(onReject)

Similar as `promise.catch`, but returns a fake promise

## await

The `FakePromise` instance could even be `await`ed

```js
console.log(await FakePromise.resolve(1))  // 1

await FakePromise.reject('error') // throw 'error'
```

## License

MIT

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