Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kaelzhang/promise-faker
Provides promise-like APIs but does the synchronous things.
https://github.com/kaelzhang/promise-faker
fake faker nodejs promise sync synchronous
Last synced: 15 days ago
JSON representation
Provides promise-like APIs but does the synchronous things.
- Host: GitHub
- URL: https://github.com/kaelzhang/promise-faker
- Owner: kaelzhang
- License: other
- Created: 2018-01-09T03:00:19.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-22T02:59:15.000Z (almost 7 years ago)
- Last Synced: 2024-12-06T05:16:33.469Z (23 days ago)
- Topics: fake, faker, nodejs, promise, sync, synchronous
- Language: JavaScript
- Size: 13.7 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE-MIT
Awesome Lists containing this project
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)# 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
// 1FakePromise.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)
// 1FakePromise.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)) // 1await FakePromise.reject('error') // throw 'error'
```## License
MIT