Giter VIP home page Giter VIP logo

promise-faker's Introduction

Build Status Coverage

promise-faker

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

Install

$ npm install promise-faker

Usage

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:

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.
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.

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 awaited

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

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

License

MIT

promise-faker's People

Contributors

kaelzhang avatar

Stargazers

 avatar Colin Wang avatar

Watchers

 avatar James Cloos avatar  avatar

promise-faker's Issues

Could you provide a version (please) that's compatible with node out of the box?

At one point, I had this lib working in our node tests, but only by specifically requiring promise-faker/src. Then I reinstalled node modules (so this dependency must've been updated) and now promise-faker/src/index is import/export based, but lib/index requires babel-runtime which is not listed under dependencies, so that fails too. Please, please, please . . . make node a first class citizen. If you then want to compile to something else for the browser, that's fine.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.