GithubHelp home page GithubHelp logo

d-mobilelab / promiselite Goto Github PK

View Code? Open in Web Editor NEW
2.0 5.0 0.0 740 KB

PromiseLite is a light, browser-friendly implementation of JavaScript promises.

License: MIT License

JavaScript 39.90% CSS 6.74% HTML 53.36%

promiselite's Introduction

promiselite

[!!!] This repository has been deprecated, we suggest to use https://github.com/taylorhakes/promise-polyfill instead

Build Status Coverage Status npm version Bower version GitHub version

PromiseLite is a light, browser-friendly implementation of JavaScript promises.

Installation

NPM

npm install --save promiselite

You can found the library ready for production on node_modules/promiselite/dist/dist.js

Bower

bower install --save promiselite

You can found the library ready for production on bower_components/promiselite/dist/dist.js

Documentation

To read documentation, go to:

http://d-mobilelab.github.io/promiselite/1.3.0

Replace 1.3.0 with the version of the documentation you want to read.

Usage

Executors, then and fail

See the documentation of JavaScript Promise here.

The name fail was used instead of catch for compatibility reasons. since some browsers do not allow bare keywords in dot notation.

Example

#!JavaScript

var p = new PromiseLite(function(resolve, reject){
    setTimeout(function(){
        if (Math.random() > 0.5){
            resolve('It worked!');
        } else {
            reject('It failed!');
        }
    });
}).then(function(value){
    console.info('Yay! ' + value); // outputs 'Yay! It worked!'
}).fail(function(reason){
    console.error('Ops! ' + reason); // outputs 'Ops! It failed!'
});;

Forced blocks

The method force corresponds to a finally block applied to a chain of promises. It is executed both when a PromiseLite instance is both in a fulfilled or rejected state.

The name force was used instead of finally for compatibility reasons since some browsers do not allow bare keywords in dot notation.

Example

#!javascript

var p = new PromiseLite();

p.then(function(value){
   // do something if resolved
   // i.e. read data from an open file
}).fail(function(reason){
   // do something if rejected (or if an Error is raised)
   // i.e. show an error message on screen - "missing file"
}).force(function(){
   // do something both if resolved or rejected
   // i.e. close the file
});

Resolving a Promise created without an executor

#!JavaScript

var p = new PromiseLite();

p.then(function(value){
    console.log('The answer is ' + value);
});

// since p has no executor, it won't be resolved until we invoke p.resolve

p.resolve(42); // outputs 'The answer is 42'

PromiseLite.all

PromiseLite.all takes an Array of PromiseLite as argument and is resolved if and only if all such promises are fulfilled.

#!javascript

var p1 = new PromiseLite();
var p2 = new PromiseLite();
var p3 = new PromiseLite();

var pAll = new PromiseLite.all([p1, p2, p3]);

PromiseLite.race

PromiseLite.race takes an Array of PromiseLite as argument, then

  • if the first promise that is settled is fulfilled, the promise returned by PromiseLite.race is resolved

  • if the first promise that is settled is rejected, the promise returned by PromiseLite.race is rejected

#!javascript

var p1 = new PromiseLite();
var p2 = new PromiseLite();
var p3 = new PromiseLite();

var pRace = new PromiseLite.race([p1, p2, p3]);

PromiseLite.any

PromiseLite.any takes an Array of PromiseLite as argument, then

  • if at least one of such promises is fulfilled, the promise returned by PromiseLite.any is resolved

  • if all the promises are rejected, the promise returned by PromiseLite.any is rejected

#!javascript

var p1 = new PromiseLite();
var p2 = new PromiseLite();
var p3 = new PromiseLite();

var pAny = new PromiseLite.any([p1, p2, p3]);

promiselite's People

Contributors

ffognani avatar aureliome avatar fabfog avatar lore96 avatar

Stargazers

 avatar Massimo Zedda avatar

Watchers

James Cloos avatar Francesco Belli avatar Pasquale Mangialavori avatar  avatar Massimo Zedda avatar

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.