npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

logical-promise

v1.0.2

Published

Logical operators on Promises

Downloads

2

Readme

Build Status Coverage

logical-promise

logical-promise uses template literals to perform logical operators on Promises.

logical-promise will change the way you write Promises.

Supported operators:

  • Logical AND: &&
  • Logical OR: ||
  • Logical NOT: !
  • Parenthese: ( and )
  • Conditional(ternary) operators: ? and :

Install

$ npm install logical-promise

Examples

import logical, {
  FULLFILLED,
  FULLFILLED_AND_TRUE
} from 'logical-promise'

const resolve = n => Promise.resolve(n)
const reject = n => Promise.reject(n)

1. By default, if rejected, then go into logical OR

logical `${reject(1)} || ${resolve(0)}`
// Promise.resolve(0)

2. Specify condition method. Equivalent to example 1.

logical(FULLFILLED) `${reject(1)} || ${resolve(0)}`
// Promise.resolve(0)

3. Change the behavior using condition method

logical `
  ${reject(1)}
  || ${resolve(0)}
  || ${resolve(2)}
`
// Promise.resolve(0)

logical(FULLFILLED_AND_TRUE) `
  ${reject(1)}
  || ${resolve(0)}
  || ${reject(2)}
`
// Promise.reject(2)

4. Uses factory functions that return promises.

import {
  factory
} from 'logical-promise'

factory `
  ${() => Promise.reject(1)}
  && ${() => Promise.resolve(0)}
`
// Promise.reject(1)

By using factory, we could prevent unnecessary Promise branch from executing. For example:

import delay from 'delay'

let obj
const p1 = () => delay(10).then(() => {
  obj = {a: 1}
})
const p2 = () => obj.a
logical `${p1()} && ${p2()}`   // Booooooooooooom !!!

If use factory:

obj = null

factory `${p1} && ${p2}`       // Promise.resolve(1)

5. You could also use condition method with factory

factory(FULLFILLED_AND_TRUE) `
  ${() => reject(1)}
  || ${() => resolve(0)}
  || ${() => resolve(2)}
`
// Promise.resolve(2)

6. Ternary operators (a ? b : c) are also supported

const f = n => resolve(n)

factory(FULLFILLED_AND_TRUE) `
  ${f(0)}
    ? ${f(1)}
    // We can also write comments here.
    // Even nested ternary operators are supported.
    : ${f(0)}
      ? ${f(2)}
      : ${f(3)} && ${f(4)}
`
// Promise.resolve(4)

7. Promises are not always required, but it always returns a Promise

logical(FULLFILLED_AND_TRUE) `${0} || ${resolve(1)}`
// Promise.resolve(1)

factory(FULLFILLED_AND_TRUE) `${() => 0} || ${() => 1}`
// Promise.resolve(1)

Usage

logical(operators, ...promises)

logical `template`
logical(condition) `template`
factory `template`
factory(condition) `template`
  • condition function(promise: Promise): Boolean The method to check whether a expression should be treated as true.

Returns Promise

Built-in condition methods.

FULLFILLED

const FULLFILLED = promise => promise.then(
  () => true,
  () => false
)

FULLFILLED_AND_TRUE

const FULLFILLED_AND_TRUE = promise => promise.then(
  result => !!result,
  () => false
)

How does it work

If you have figured it out how template literals works, then it is easy to find out that the example 1 is equivalent to:

// Spaces will be trimmed
logical(['', ' || ', ''], reject(1), resolve(0))

Template literals do a great job to tokenize the source code for us 😆 ~

License

MIT