promise-faker
Version:
Provides promise-like APIs but runs synchronously. This module is useful for controlling flows.
122 lines (85 loc) • 2.92 kB
Markdown
[](https://travis-ci.org/kaelzhang/promise-faker)
[](https://codecov.io/gh/kaelzhang/promise-faker)
<!-- optional appveyor tst
[](https://ci.appveyor.com/project/kaelzhang/promise-faker)
-->
<!-- optional npm version
[](http://badge.fury.io/js/promise-faker)
-->
<!-- optional npm downloads
[](https://www.npmjs.org/package/promise-faker)
-->
<!-- optional dependency status
[](https://david-dm.org/kaelzhang/promise-faker)
-->
Provides `Promise`-like APIs but runs synchronously. This module is useful for controlling flows.
```sh
$ npm install promise-faker
```
```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
```
- **executor** `Function(resolve, reject)`
Returns a fake promise
- **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'
}
```
Similar as `Promise.reject`, but returns a fake promise
Similar as `Promise.all`, but returns a fake promise
Similar as `promise.then`, but returns a fake promise
Similar as `promise.catch`, but returns a fake promise
The `FakePromise` instance could even be `await`ed
```js
console.log(await FakePromise.resolve(1)) // 1
await FakePromise.reject('error') // throw 'error'
```
MIT