Socket
Book a DemoInstallSign in
Socket

nok-promise

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nok-promise

Lightweight JavaScript Promise

1.0.2
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

JavaScript Promise

JavaScript Promise implementation based on John Hann's Promise gist.

Usage

var doSomething = function(data){
  // instantiating a promise
  var promise = new Promise;
  
  // some function that accepts success and error handlers
  process({
    data: data,
    success: function(result){
      promise.resolve(result);
    },
    error: function(error){
      promise.reject(error)
    }
  });
  
  // returning 'limited' version of promise
  return promise.limited();
};

doSomething().then(function onResolve(result){
  // handle result
}, function onReject(error){
  // handle error
});

Nested Promises

var promiseA = new Promise,
    promiseB = new Promise,
    nestedPromise = new Promise(promiseA, promiseB).success(function(value) {
        alert(value.join(" "));
    });

promiseA.resolve("nestedPromise");
promiseB.resolve("resolved"); /* alerts "nestedPromise resolved" */

Nested Promises with Functions as arguments

var _promiseA = new Promise,
    _promiseB = new Promise,
    functionThatReturnsPromiseA = function() {
        return _promiseA;
    },
    functionThatReturnsPromiseB = function() {
        return _promiseB;
    },
    nestedPromise = new Promise(functionThatReturnsPromiseA, functionThatReturnsPromiseB).success(function(value) {
        alert(value.join(" "));
    });

_promiseA.resolve("nestedPromise");
_promiseB.resolve("resolved"); /* alerts "nestedPromise resolved" */

Filtered Promises chain

    var promise = new Promise();

    var asyncFunction_1 = function(arg) {
        var p = new Promise;

        setTimeout(function() {
            p.resolve(arg + 1);
        }, 1000);

        // return promise to continue the chain
        return p;
    };

    var asyncFunction_2 = function(arg) {
        var p = new Promise;

        setTimeout(function() {
            p.resolve(arg + 2);
        }, 1000);

        return p;
    };

    var syncFunction = function(arg) {
        alert(arg + 3);
    };

    // chain async and sync functions
    promise.then(asyncFunction_1).then(asyncFunction_2).then(syncFunction);

    promise.resolve(1); /* two seconds later alerts "7" */

Credits to @solid_coder and @unscriptable

Keywords

promise

FAQs

Package last updated on 13 Dec 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.