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

v0.0.3

Published

Execute a batch of Promises and test them as they come in. Exit early when one fails.

Downloads

1

Readme

promise-trial

Execute a batch of Promises and test them as they come in. Exit quickly when one fails rather than waiting for all to resolve.

Useful for it you have a bunch of asynchronous actions you need to execute and you want to test the response of each, but if one of the tests fails you want to know immediately rather than waiting on all to resolve.

This differs from Promise.all which waits for all tests to complete before continuing, or Promise.race which only accepts the first that returns.

Install

npm install --save promise-trial

Usage

The module accepts an Array of Objects which must contain two properties: promise and comparitor. The promise key should have a corresponsing Promise value and the comparitor should be a function which returns a Boolean based on the Promise's resolved value. An example object might look like:

var myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve(2);
  }, 200);
});

function myComparitor(arg) {
  return arg % 2 === 0;
}

var myTrialObject = {
  promise: myPromise,
  comparitor: myComparitor
};

Once you have your object(s) setup you pass them in to the trial function as demonstrated below. You can pass in any number of objects for testing in a batch.

import trial from 'promise-trial';

trial([myTrialObject])
  .then(resp => {
    // Perform application logic
    // after test completes
  });

This example only has one Promise to test, but it will resolve to true and then the application can proceed.

Responses

The trial function returns a Promise which will resolve in one of three ways:

  • All test comparitors returns true, the trial will resolve
  • One or more tests were rejected but some could complete, the trial will resolve
  • A comparitor returned false, the trial will resolve

Passing Response

If all the tests pass or if at least one Promise rejects, the response object will look be the following object:

{
  count: <Number>       // The number of items tested
  resolvedData: <Array> // An array of all data resolved from the Promises
  rejected: <Array>     // An array of all rejected Promises with their Error object
  passed: <Boolean>     // A boolean whose value will be `true` since all tests passed
}

This object contains all the necessary to carry on application logic. All data in the resolvedData array mirrors the order of the array passed in to the trial. For any trial that rejects a null value will fill in at the proper index of resolvedData.

Failing Response

Whenever a comparitor returns false the trial will immediately resolve and so that your application can proceed quickly rather than waiting for all Promises to resolve. The response object for a failing test is simple:

{
  passed: <Boolean>     // A boolean set to `false`
  failedIndex: <Number> // The index of the failed test
}

Rejection

A trial will not reject unless a malformed test object is passed in. Ensure that your objects contain two the promise and comparitor properties

Arguments In Comparitors

If you need to pass arguments into each trial you can do so as additional arguments to the trial function. Arguments will be passed into the comparitor function in the same order that they're passed into the trial function. For example:

import trial from 'promise-trial';

trial([myTrialObject], 'arg1', 'arg2')
  .then(resp => {
    // Perform application logic
    // after test completes
  });

Contribution

Creat a pull request with a failing unit test, and I'll gladly help fix it