🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

promise

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
p

promise

Bare bones Promises/A+ implementation

8.3.0
latest
100

Supply Chain Security

100

Vulnerability

100

Quality

79

Maintenance

100

License

Uses eval

Supply chain risk

Package uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.

Found 1 instance in 1 package

Version published
Weekly downloads
17M
0.14%
Maintainers
3
Weekly downloads
 
Created
Issues
20

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

FAQs

Package last updated on 25 Oct 2022

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