Socket
Socket
Sign inDemoInstall

@kwsites/promise-deferred

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kwsites/promise-deferred

Minimalist creation of promise wrappers, exposing the ability to resolve or reject the inner promise


Version published
Maintainers
1
Created

What is @kwsites/promise-deferred?

@kwsites/promise-deferred is a utility package that provides a simple way to create deferred promises. This can be useful in scenarios where you need to create a promise and resolve or reject it at a later time, outside of the initial promise executor function.

What are @kwsites/promise-deferred's main functionalities?

Creating a Deferred Promise

This feature allows you to create a deferred promise that can be resolved or rejected at a later time. The code sample demonstrates creating a deferred promise, simulating an asynchronous operation with setTimeout, and resolving the promise after 1 second.

const { deferred } = require('@kwsites/promise-deferred');

const def = deferred();

// Simulate an asynchronous operation
setTimeout(() => {
  def.resolve('Success!');
}, 1000);

// Use the promise
async function asyncOperation() {
  const result = await def.promise;
  console.log(result); // Outputs: 'Success!'
}

asyncOperation();

Handling Deferred Promise Rejection

This feature allows you to handle the rejection of a deferred promise. The code sample demonstrates creating a deferred promise, simulating an asynchronous operation with setTimeout, and rejecting the promise after 1 second. The async function catches and logs the error.

const { deferred } = require('@kwsites/promise-deferred');

const def = deferred();

// Simulate an asynchronous operation
setTimeout(() => {
  def.reject(new Error('Failure!'));
}, 1000);

// Use the promise
async function asyncOperation() {
  try {
    const result = await def.promise;
  } catch (error) {
    console.error(error.message); // Outputs: 'Failure!'
  }
}

asyncOperation();

Other packages similar to @kwsites/promise-deferred

FAQs

Package last updated on 01 Jul 2020

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