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

promise-sugar

v2.3.3

Published

Promise syntactic sugar - promise chains without write ".then"

Downloads

55

Readme

Promise syntactic sugar Build Status

No need to write .then in your promise chains. The promise is the .then function itself!

What it does

1 ) It allows you to convert this

Promise.resolve(10)
.then((n) => n / 2)
.then((n) => n * 3)
.then(log) // -> 15
.catch(logError)

into this

sweeten(10)
((n) => n / 2)
((n) => n * 3)
(log) // -> 15
(null, logError) // or .catch(logError)

2 ) and this

// Given two existing promises A and B
// wait for A and B and return B's value
log(await A.then(() => B)) // -> B's value

into this

log(await A(B)) // -> B's value

where

const log      = console.log.bind(console);
const logError = console.error.bind(console);

That's almost it! For More sugar see below.

A sweeten promise is still a thenable. Thus you can still await for it, or you could swap the Promise with the sweeten version in your old code and it should still work the same.

Promise-sugar tries to preserve all other behaviors of the Promise library used.

There is another library that implements a similar paradigm - thunks.

Thunks is different from Promise-sugar and more complex (a thunk is not a promise and it has no .catch() method).

You can play with it on jsBin

Install

  • Copy promise-sugar.js to your project or install it using npm:
npm install promise-sugar --save
  • Import promise-sugar.js into your app using import (ESM), require (AMD or CommonJs) or as a script tag.
import sweeten from 'promise-sugar';
const sweeten = require('promise-sugar');
<script src="https://unpkg.com/promise-sugar"></script>
  • Make sure there is a Promise implementation or get a polyfill like es6-promise.
sweeten.usePromise(require('es6-promise').Promise); // polyfill

More sugar

Regardless of the Promise implementation used, all sweeten promises have the following methods:

sweeten(promise)
    .catch(onReject)   // Promite/A+
    .finally(callback) // not a Promise/A+ method
    .timeout(1000)     // reject in 1 sec, not a Promise/A+ method

If Promise.prototype.progress is defined, Promise-sugar will preserve it.

Here are some helper methods of Promise-sugar:

sweeten.when(value_or_thenable); // creates a sweeten promise
let deferred = sweeten.defer();  // creates a deferred with a sweeten .promise

sweeten.allValues(obj);          // Similar to Promise.all(list), but accepts an object with thenable values

if(sweeten.isThenable(any)) any.then(doStuff);

let waiter = sweeten.wait(123);  // setTimeout()
waiter.then(doStuffLater);
waiter.stop();                   // don't doStuffLater() (like clearTimeout())


function sum(a,b) { return a + b; }
let ssum = sweeten.fn(sum); // sweeten version of sum()
ssum(2, Promise.resolve(3))(log); // -> 5


// Promise/A+ sweet equivalents
sweeten.resolve(val)
sweeten.reject(val)
sweeten.race(list)
sweeten.all(list)
sweeten.any(list)
sweeten.allSettled(list)

Examples

Sweeten promises are just promises and functions (thens) at the same time:

let result = sweeten(fetch('/my/api'))
             ((res) => res.json())
;

// Now you have a simple function that contains your result
result(log);

// and it is still a promise!
result.catch(logError);

// and can be used as such
Promise.all([result, fetch('my/api/something/else')])
.then(/*...*/);

// or equivalent of the above
sweeten.all([result, fetch('my/api/something/else')])
(/*...*/);

Sweeten promise constructor:

let myStuff = new sweeten(function (resolve, rejext){
    setTimeout(resolve, 100, Math.random());
});

myStuff((myNumber) => {/*...*/}, (error) => {/*...*/});