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-conveyor

v1.1.1

Published

Pipe data down the promise chain.

Downloads

6

Readme

Promise Conveyor

A library to pipe data down a promise chain. Similar to, yet different from async.waterfall. API documentation is this way.

What is this for?

If you have a series of tasks that all depend on successful execution of all previous tasks, and any of them may or may not be asynchronous, then this library is for you. Promise Conveyor allows you to chain operations in a way that output of any previously executed operation can be available in any of the following tasks. Simply put, it does something like this:

someAsyncOperation()
  .then(
    function(data) {
      anotherAsyncOperation()
        .then(function(result) {
          data.property1 = result;
          return data;
        });
    })
  .then(
    function(data) {
      yetAnotherAsyncOperation()
        .then(function(result) {
          data.property2 = result;
          return data;
        });
    })

Messy, isn't it? Well, Promise Conveyor can clean it up this way:

new Conveyor()
  .then(someAsyncOperation)
  .then(anotherAsyncOperation)
  .then(yetAnotherAsyncOperation)
  .catch(errorHandler)

Better? This is only the tip of the iceberg. Read on to find out more awesome features.

Piping Data

Let's say, you want to find a user from the database, authenticate and then display user's information. You have the following steps to run:

  1. Find the user record by some criteria.
  2. Compare user supplied password with the one stored in database.
  3. Get user's information and format it for display.

As you can see, both step 2 and step 3 need data produced by step 1. Meanwhile, step 3 should only execute if step 2 is successful. Following is the Promise Conveyor way of doing this:

new Conveyor({password: '...', email: '...'})
  .then({input: 'email', output: 'user'}, db.findUser)
  .then({input: ['user', 'password']}, crypto.compare)
  .then({input: 'user'}, formatUserInfo)
  .catch(somethingWentWrong)

So let me explain what this code does:

  1. This line creates a new conveyor and makes email and password available for its conveyor steps.
new Conveyor({password: '...', email: '...'})
  1. This line appends the db.findUser function as a conveyor step, and tells conveyor to use email as the function's argument, and output the returned value to the user. db.findUser can either return a user object or a promise that resolves with a user object.
  .then({input: 'email', output: 'user'}, db.findUser)
  1. This line is similar to the previous one, but in this case crypto.compare accepts 2 arguments, therefore input property of the config object is an array: ['user', 'password']. Note that it does not have an output defined: if crypto.compare throws an exception or returns a rejected promise, the entire conveyor would terminate and the error callback will be called.
  .then({input: ['user', 'password']}, crypto.compare)
  1. This line tells conveyor to call formatUserInfo, and pass in user as the argument. As you can see here, the user value produced by line 2 is still available here.
  .then({input: 'user'}, formatUserInfo)
  1. This line defined an error callback, which will be called if any step in conveyor encounters an error.
  .catch(somethingWentWrong)

Input/Output Inference

In some cases, you can omit input and output values, and Conveyor will try to determine what they are. For example:

increment = function(a) { return a + 1; };

new Conveyor(data: 0)
  .then({input: 'data'}, increment)
  .then(increment)
  .then(increment)
  .then(increment)

In this case, the first increment step will accept the data as its argument, increment it by 1, and output it back to data. Then the next increment will get the output value of the last step, and output back to it. This allows very convenient creation of long chains of transforms that operate on the same object.

Precise rules of inference are the following:

  • Input
  • if this is the first conveyor step, defaults to null (passes all available data)
  • if last step has an output, then uses that output
  • if last step did not output anything, then defaults back to null
  • Output
  • if this is the first conveyor step, does not output anything
  • if this step has one single input value, outputs there
  • if this step has multiple input values, outputs nothing

Config Object

As you have seen from the previous examples, the then function of the Conveyor accepts an optional config object and a function. You can add any information you wish to pass to your function into the config object, and it will be accessible as this.config inside conveyor steps.

Similarly, you can access the Conveyor object using this.conveyor.

License

The MIT License

Copyright (c) 2014-2015 Cellaflora Design LLP, Denis Luchkin-Zhou

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.