promise-faker
Provides Promise
-like APIs but runs synchronously. This module is useful for controlling flows.
Install
$ npm install promise-faker
Usage
// Write flows as normal Promise does { const result = p // Not to make the following chain. return p} // Then, run them as synchronous flows // 2 // Promise {2}
FakePromise actually runs synchronously:
Promiseconsole// 2// 1 FakePromiseconsole// 3// 4
new FakePromise(executor)
- executor
Function(resolve, reject)
Returns a fake promise
FakePromise.resolve(subject [, end])
- end
?boolean=false
The additional parameter only forFakePromise
, and if this parameter istrue
, it will try to get the final value or throw an error if there is a rejection.
FakePromise// 1 FakePromise// -> throw '2'
And if the fake promise is still pending, an Error('pending unexpectedly')
error will thrown.
const p = { return 1} try FakePromise catch e console // '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
console // 1 await FakePromise // throw 'error'
License
MIT