Socket
Socket
Sign inDemoInstall

value-or-promise

Package Overview
Dependencies
253
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    value-or-promise

A thenable to streamline a possibly sync / possibly async workflow.


Version published
Maintainers
1
Created

Package description

What is value-or-promise?

The 'value-or-promise' npm package is designed to handle values that could either be a direct value or a promise that resolves to a value. It provides utility functions to work with such values or promises in a consistent way, without having to check the nature of the value each time.

What are value-or-promise's main functionalities?

Handling values or promises uniformly

This feature allows you to handle both direct values and promises using the same API, which simplifies the code when the nature of the value is not known in advance or can vary.

const { ValueOrPromise } = require('value-or-promise');

function getValueOrPromise(value) {
  return new ValueOrPromise(() => value)
    .then(v => v)
    .resolve();
}

// Usage with a direct value
getValueOrPromise(42).then(console.log); // 42

// Usage with a promise
getValueOrPromise(Promise.resolve(42)).then(console.log); // 42

Chaining operations

This feature allows chaining multiple operations on a value or promise, with each operation being applied as the previous one resolves. It's similar to promise chaining but works with non-promise values as well.

const { ValueOrPromise } = require('value-or-promise');

function chainOperations(value) {
  return new ValueOrPromise(() => value)
    .then(v => v * 2)
    .then(v => v + 1)
    .resolve();
}

// Usage with a direct value
chainOperations(10).then(console.log); // 21

// Usage with a promise
chainOperations(Promise.resolve(10)).then(console.log); // 21

Error handling

This feature provides a way to handle errors that may occur during the processing of a value or promise, similar to how errors are caught in promise chains.

const { ValueOrPromise } = require('value-or-promise');

function handleErrors(value) {
  return new ValueOrPromise(() => value)
    .then(v => {
      if (v < 0) throw new Error('Negative value');
      return v;
    })
    .catch(error => 'Error: ' + error.message)
    .resolve();
}

// Usage with a direct value
handleErrors(-10).then(console.log); // Error: Negative value

// Usage with a promise
handleErrors(Promise.resolve(-10)).then(console.log); // Error: Negative value

Other packages similar to value-or-promise

Changelog

Source

1.0.4

Patch Changes

  • a4eff20: add missing catch

Readme

Source

value-or-promise

A thenable to streamline a possibly sync / possibly async workflow.

FAQs

Last updated on 27 Apr 2021

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