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.extra

v4.0.0

Published

series, waterfall and more with vanilla es promise/thenable.

Downloads

1,210

Readme

Build Status Coverage

promise.extra

Promise.series and Promise.waterfall with vanilla es Promise/thenable

Install

$ npm i promise.extra

Usage

import {
  series,
  waterfall,
  reduce,
  factory
} from 'promise.extra'

series([1, 2, 3], (prev, v) => Promise.resolve(v))
.then(values => {
  console.log(values)  // [1, 2, 3]
})

waterfall([1, 2, 3], 0, (prev, v) => Promise.resolve(prev + v))
.then(result => {
  console.log(result)  // 6
})

// The reducer could even returns non-promise values
waterfall(
  [1, 2, 3],
  0,
  (prev, v, i) => i > 0
    ? prev + v
    : Promise.resolve(prev + v)
)
.then(result => {
  console.log(result)  // 6
})

reduce([v1, v2, v3], reducer, initValue)

series(tasks, reducer?): Promise

  • tasks Array<Task> an array of tasks
    • Task any each of the tasks could be anything.
  • reducer ?ReducerFunction the reducer which will process each task and returns either a Promise or any value.

Always returns a Promise.

function ReducerFunction (
  prev: any,
  task: Task,
  index: number,
  tasks
): Promise | any
  • prev The result value of the last task which processed by the reducer. If the return value of the last reducer is an Promise, prev will be the value inside the promise.
  • task The current task.
  • index The index of task
  • tasks Just the tasks argument of function series

The default value of reducer is:


(prev, currentValue) => currentValue

currentValue is one of the Tasks, and each Task could be a Promise or a normal JavaScript variable or object.

If you want each of the Tasks to be a factory function that returns a Promise or normal values, and execute each value inside the reducer, just define the reducer as:

function reducer (prev, factory) {
  // If `series` is invoked by `series.call(this, ...args)`,
  // reducer could share the `this` object.
  return factory.call(this, prev)
}

waterfall(tasks, initValue, reducer?)

  • tasks Array<PromiseFactory>
    • PromiseFactory Function(x): Promise a factory function which receives a parameter and returns a Promise
  • initValue any | undefined optional initial value which will be passed into the first factory function.
  • reducer

Always returns Promise.

reduce(tasks, reducer, initValue)

  • tasks
  • reducer Function(prev, factory, currentIndex, tasks): Promise The reducer function
  • initValue any | undefined if no initial value is supplied, undefined will be used.

Always returns Promise

findIndex(tasks, matcher, reducer?)

find(tasks, matcher, reducer?)

indexOf(tasks, value, reducer?)

some(tasks, reducer?)

every(tasks, reducer?)

Each of the methods is similar to the behavior of Array.prototype.<method>.

factory(promise)

  • promise Promise|PromiseLike

Creates the new reduce, series, waterfall with the Promise which can be bluebird, promise-faker or something.

import Promise from 'promise-faker'
const {
  series
} = factory(Promise)

series(tasks)

Examples

Usage of reducer

const nickName = 'Steve'

// Suppose there are two async functions to check the nickName
series(
  [checkNickNameSyntax, remoteCheckUnique],
  (prev, factory) => factory(nickName)
)

The this Object

function lessThan10 (notThrow) {
  if (this.number < 10) {
    this.number ++
    return true
  }

  if (notThrow) {
    return false
  }

  return Promise.reject('larger than 10')
}

series.call({number: 10}, [lessThan10, lessThan10])
// Reject

series.call({number: 1}, [lessThan10, lessThan10])
// Promise.resolve<true>

series.call({number: 10},
  [lessThan10, lessThan10],
  function (prev, factory) {
    // 1. Be careful that you should use `factory.call` here
    //   to pass the `this` object to `factory`
    // 2. use the parameter `notThrow`
    return factory.call(this, true)
  }
)
// Promise.resolve<false>

Upgrade Guide

3.x -> 4.x

promise.extra no longer supports node < 6 since 4.0.0. For old node versions, require('promise.extra/legacy')

License

MIT