Giter Site home page Giter Site logo

nono1526 / fidelity Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bucharest-gold/fidelity

0.0 2.0 0.0 1 MB

THIS REPO IS NO LONGER MAINTAINED: A fast and simple Promise/A+ implementation

License: MIT License

JavaScript 99.45% Makefile 0.12% Shell 0.27% HTML 0.16%

fidelity's Introduction

Promises/A+ Fidelity

Coverage Status Build Status Known Vulnerabilities dependencies Status

NPM

A fast and simple Promise/A+ implementation capable of Node.js or browser-based execution. Fidelity adheres to both the Promise/A+ specspecification, and the ES6 Promise API in its entirety.

Project Info
License: MIT
Build: make
Documentation: http://bucharest-gold.github.io/fidelity/
Issue tracker: https://github.com/bucharest-gold/fidelity/issues
Engines: Node.js 6.x, 8.x

Installation

Fidelity can be used in Node.js.

$ npm install fidelity

Or in the browser.

<!-- load fidelity -->
<script src="fidelity-promise-min.js"></script>

When used in the browser, a FidelityPromise object is created in the global scope.

Usage

A fidelity promise behaves according to the Promises/A+ specification. If you haven't read it, it's worth your time and will probably make all of the fidelity documentation clearer.

You can create promises using the exported constructor.

const Fidelity = require('fidelity');
new Fidelity( (resolve, reject) => {
  // etc.
} )

You call the constructor function with an executor function as the only parameter. Typically this function will perform some asynchronous task, and when that task has completed it will resolve or reject the promise depending on whether or not the task completed successfully.

The executor function takes two function parameters: resolve and reject. These functions are used to resolve or reject the promise as needed.

Suppose we have a function, someAsyncFunction() that takes some time to complete asynchronously. We can call this function using a promise.

const Fidelity = require('fidelity');

new Fidelity( (resolve, reject) => {
  someAsyncFunction((result, err) => {
    if (err) {
      reject(err); // The function produced an error. Reject the promise
    } else {
      resolve(result); // Fulfill the promise with the result
    }
  });
})
.then( (val) => {
  // This code executes after a promise has been fulfilled
  // Do something with the result.
})
.catch( (err) => {
  // This code executes if the promise  was rejected
});

Promise states

A promise will only ever be in one of three states. Fidelity.PENDING, Fidelity.FULFILLED or Fidelity.REJECTED.

API

Generated documentation can be found here: http://bucharest-gold.github.io/fidelity.

Fidelity

The fidelity module exports a constructor function for a Fidelity promise.

const Fidelity = require('fidelity');

new Fidelity(func)

A constructor function that creates and returns a promise. The func parameter is a function that accepts a resolve and reject function.

Fidelity#then(onFulfilled, onRejected)

The 'then' function takes two function arguments. The first, onFulfilled, is called with the return value (if any) of the promise function if it is successfully fulfilled. The second function, onRejected is called in the event of an error. A promise is returned in either case.

p.then( (result) => {
  console.log('sucessful result ', result);
}, (err) => {
  console.error('whoops!', err);
});

Fidelity#catch(onRejected)

This is just a little syntactic sugar for promise.then(null, onRejected);. It returns a promise.

Fidelity.resolve(value)

A static utility function that returns a promise which has been resolved with the provided value.

Fidelity.reject(reason)

A static utility function that returns a promise which has been rejected with the provided reason.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject

Fidelity.all(iterable)

A static utility function which returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

Fidelity.race(iterable)

A static utility function which returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race

Fidelity.deferred()

A static utility function that Creates and returns a deferred object. Deferred objects contain a promise which may be resolved or rejected at some point in the future.

An example.

const deferred = Fidelity.deferred();

callSomeAsyncFunction((err, result) => {
  if (err) {
    deferred.reject(err);
  } else {
    deferred.resolve(result);
  }
});

Fidelity.deferred().resolve(value)

Resolves the deferred promise with value.

Fidelity.deferred().reject(cause)

Rejects the deferred promise with cause.

Fidelity.deferred().promise

The deferred promise.

Testing

This module passes all of the tests in the Promises/A+ Compliance Test Suite. To run the full suite of the Promises/A+ spec, just npm test from the command line.

Contributing

We encourage community contributions! Please read the contributing guide if you would like to contribute to this project.

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.