Socket
Socket
Sign inDemoInstall

promise

Package Overview
Dependencies
1
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    promise

Bare bones Promises/A+ implementation


Version published
Maintainers
1
Install size
12.1 kB
Created

Package description

What is promise?

The 'promise' npm package provides a robust library for working with promises in JavaScript. It allows for the creation, manipulation, and composition of promises for asynchronous operations. It offers features such as deferreds, helpers for common patterns, and methods for controlling the flow of promise chains.

What are promise's main functionalities?

Creating a new Promise

This feature allows you to create a new promise. The constructor takes a function that contains the asynchronous operation. The function provides two arguments, resolve and reject, which are used to settle the promise.

var Promise = require('promise');

var promise = new Promise(function (resolve, reject) {
  // Asynchronous operation here
  if (/* operation successful */) {
    resolve('Success!');
  } else {
    reject(Error('Failure.'));
  }
});

Promise Chaining

Promise chaining enables you to link together multiple asynchronous operations. Each .then() returns a new promise, allowing for further chaining. The .catch() method is used for error handling.

promise.then(function(result) {
  console.log(result); // 'Success!'
  return 'Another value';
}).then(function(result) {
  console.log(result); // 'Another value'
}).catch(function(error) {
  console.error(error);
});

Deferreds

Deferreds provide a way to expose the resolve and reject functions of a promise outside of its constructor. This can be useful when the resolution of a promise needs to be controlled externally.

var deferred = Promise.defer();

deferred.promise.then(function(result) {
  console.log(result);
});

deferred.resolve('Deferred success!');

Promise.all

Promise.all is used to wait for all promises in an iterable to be resolved. The resulting promise is resolved with an array of the resolved values from the passed promises.

var p1 = Promise.resolve(3);
var p2 = 1337;
var p3 = new Promise(function (resolve, reject) {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([p1, p2, p3]).then(function(values) {
  console.log(values); // [3, 1337, 'foo']
});

Promise.race

Promise.race is used to wait only for the first promise to be resolved or rejected. It resolves or rejects with the value from that promise.

var p1 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 500, 'one'); 
});
var p2 = new Promise(function(resolve, reject) { 
    setTimeout(resolve, 100, 'two'); 
});

Promise.race([p1, p2]).then(function(value) {
  console.log(value); // 'two'
});

Other packages similar to promise

Readme

Source

Build Status

promise

This a bare bones Promises/A+ implementation.

It is designed to get the basics spot on correct, so that you can build extended promise implementations on top of it.

Installation

Server:

$ npm install promise

Client:

$ component install then/promise

API

In the example below shows how you can load the promise library (in a way that works on both client and server). It then demonstrates creating a promise from scratch. You simply call new Promise(fn). There is a complete specification for what is returned by this method in Promises/A+.

var Promise = require('promise');

var promise = new Promise(function (resolve, reject) {
    get('http://www.google.com', function (err, res) {
      if (err) reject(err);
      else resolve(res);
    });
  });

Extending Promises

There are three options for extending the promises created by this library.

Inheritance

You can use inheritance if you want to create your own complete promise library with this as your basic starting point, perfect if you have lots of cool features you want to add. Here is an example of a promise library called Awesome, which is built on top of Promise correctly.

var Promise = require('promise');
function Awesome(fn) {
  if (!(this instanceof Awesome)) return new Awesome(fn);
  Promise.call(this, fn);
}
Awesome.prototype = Object.create(Promise.prototype);
Awesome.prototype.constructor = Awesome;

//Awesome extension
Awesome.prototype.spread = function (cb) {
  return this.then(function (arr) {
    return cb.apply(this, arr);
  })
};

N.B. if you fail to set the prototype and constructor properly or fail to do Promise.call, things can fail in really subtle ways.

Wrap

This is the nuclear option, for when you want to start from scratch. It ensures you won't be impacted by anyone who is extending the prototype (see below).

function Uber(fn) {
  if (!(this instanceof Uber)) return new Uber(fn);
  var _prom = new Promise(fn);
  this.then = _prom.then;
}

Uber.prototype.spread = function (cb) {
  return this.then(function (arr) {
    return cb.apply(this, arr);
  })
};

Extending the Prototype

In general, you should never extend the prototype of this promise implimenation because your extensions could easily conflict with someone elses extensions. However, this organisation will host a library of extensions which do not conflict with each other, so you can safely enable any of those. If you think of an extension that we don't provide and you want to write it, submit an issue on this repository and (if I agree) I'll set you up with a repository and give you permission to commit to it.

License

MIT

FAQs

Last updated on 05 Mar 2013

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc