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

rsp

v0.0.10

Published

Radically Simplifed Promises - call methods on values in the future

Downloads

43

Readme

node rsp

Radically Simplifed Promises.

Known Vulnerabilities

A proof-of-concept module that allows you to call methods on promises in the future:

var promise2 = promise1.toUpperCase();
var promise3 = promise2.split('').join('-');

Instead of:

promise2 = new Promise(function (resolve, reject) {
  promise1.then(function (value) {
    resolve(value.toUpperCase());
  });
});
promise3 = new Promise(function (resolve, reject) {
  promise2.then(function (value) {
    resolve(value.split('').join('-'));
  });
});

This is work in progress - more features to come.

I'll try to avoid any breaking changes to the API but while it is still 0.0.x the API may change, once it hits 1.0 then the usual semver rules will apply.

Certainly don't use any undocumented features because those are very likely to change any time.

How it works

If you have a promise p that will eventually resolve to some value then you can create another promise by calling rsp(p) that will also eventually resolve to the same value but you can call methods on it in the future that will give you promises of return values of those method calls on the future resolved value as soon as it is available.

It is easier to explain on an example:


// You have a promise `p1` that will resolve to `"text"` after 5 seconds:
var p1 = new Promise(function (resolve, reject) {
  setTimeout(function () {
    resolve("text");
  }, 5000);
});

// You create a magic promise `p2`:
var p2 = rsp(p1);

// Now you can make it uppercase:
var p3 = p2.toUpperCase();

// Then you can split it:
var p4 = p3.split('');

// You can filter it to remove 'E':
var p5 = p4.filter(function (a) { return a != 'E'; });

// You can map it to wrap in paretheses:
var p6 = p5.map(function (a) { return '(' + a + ')'; });

// Then you can join it with dashes:
var p7 = p6.join('-');

// And now you have a promise `p5` that will eventually resolve
// to '(T)-(X)-(T)' after 5 seconds but can be prepared way before
// the original promise `p1` itself is resolved.
p7.then(function (value) {
    console.log("p7 value is: " + value);
});

See: example.js for more examples.

Installation

Install to use in your project, updating the dependencies in package.json:

npm install rsp --save

It currently has one dependency: harmony-proxy to translate the old Proxy API (that both Node.js as of 4.0.0 and io.js as of v2.3.4 currently use) to the new API defined in the ECMAScript 2015 (ES6) standard. (Future versions of the rsp module should drop this dependency and use the available API directly.)

Additionally both Node.js and io.js need to be run with --harmony-proxies for proxies to be available at all. Using --harmony is not enough.

Usage

var rsp = require('rsp');

This module uses Harmony Proxies. You need to run node with --harmony-proxies

For example you can run:

node --harmony-proxies your-script.js

Or you can start your script with:

#!/bin/sh
":" //#; exec /usr/bin/env node --harmony-proxies "$0" "$@"

/* ... your JavaScript code ... */

to be able to run it as:

./your-script.js

Issues

For any bug reports or feature requests please post an issue on GitHub.

Author

Rafał Pocztarski - https://github.com/rsp

License

MIT License (Expat). See LICENSE.md for details.