Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

when

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

when

A lightweight Promises/A+ and when() implementation, plus other async goodies.

  • 3.7.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
711K
decreased by-44.19%
Maintainers
1
Weekly downloads
 
Created

What is when?

The 'when' npm package is a robust library for working with asynchronous programming in JavaScript, particularly using promises. It provides utilities for creating, managing, and composing promises, making it easier to handle asynchronous operations and their potential complexities.

What are when's main functionalities?

Creating Promises

This feature allows the creation of new promises. The code sample demonstrates how to create a simple promise that resolves with 'Hello, World!' after 1 second.

const when = require('when');
const promise = when.promise(function(resolve, reject) {
  setTimeout(() => resolve('Hello, World!'), 1000);
});
promise.then(response => console.log(response));

Chaining Promises

This feature demonstrates chaining multiple promises. It shows how to perform a series of tasks sequentially, where each task starts only after the previous one has completed.

const when = require('when');
const cleanRoom = () => when.promise(resolve => resolve('Room cleaned'));
const removeTrash = () => when.promise(resolve => resolve('Trash removed'));
const winIcecream = () => when.promise(resolve => resolve('Won ice cream'));
cleanRoom()
  .then(result => {
    console.log(result);
    return removeTrash();
  })
  .then(result => {
    console.log(result);
    return winIcecream();
  })
  .then(result => console.log(result));

Handling Errors

This feature involves error handling in promises. The code sample shows how to catch and handle errors that occur during the execution of promises.

const when = require('when');
const failTask = () => when.promise((resolve, reject) => reject('Failed task'));
failTask()
  .then(result => console.log('Success:', result))
  .catch(error => console.log('Error:', error));

Other packages similar to when

Keywords

FAQs

Package last updated on 20 Feb 2017

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

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc