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

v0.0.8

Published

Utility module for functions that return promises

Downloads

322

Readme

Build Status

promise-flow

Utility module for functions that return promises.

Note that this module expects a global Promise to exist. If used in an environment that does not implement Promise, a shim should be used.

Example

import * as pf from 'promise-flow';

pf.allObject({
  key1: Promise.resolve('value from a promise'),
  key2: 'non-promise value'
}).then(function(result) {
  // result == { key1: 'value from a promise', key2: 'non-promise value' }
});

API

allObject(object)

allObject<T>(object: { [key: string]: Promise<T> | T }): Promise<{ [key: string]: T }>

Returns a promise that resolves with a copy of the input object when all values have resolved.

promise_flow.allObject({
  key1: Promise.resolve('value from a promise'),
  key2: 'non-promise value'
}).then(function(result) {
  // result == { key1: 'value from a promise', key2: 'non-promise value' }
});

series(factories)

series<T>(factories: Array<() => Promise<T> | T): Promise<Array<T>>

Takes an array of functions that will be executed in series. Each one will wait until the previous function is done.

If one of the functions returns a rejected promise or throws an error then the resulting promise will reject with that error.

promise_flow.series([
  () => Promise.resolve('value from a promise'),
  () => 'non-promise value'
]).then(function(result) {
  // result == ['value from a promise', 'non-promise value']
});

parallel(factories)

parallel<T>(factories: Array<() => Promise<T> | T): Promise<Array<T>>

Same as series but will run all functions in parallel.

map(array, closure)

map<T, U>(array: Array<T>, closure: (value: T, index: number) => Promise<U> | U): Promise<Array<U>>

Map the items in the array with a function that may return a promise.

mapSeries(array, closure)

map<T, U>(array: Array<T>, closure: (value: T, index: number) => Promise<U> | U): Promise<Array<U>>

Same as map but will run all functions in series.

filter(array, closure)

filter<T>(array: Array<T>, closure: (value: T, index: number) => Promise<boolean> | boolean): Promise<Array<T>>

Filter the array using a function that may return a promise.

To keep a value in the array, return a promise that resolves with a truthy value (or return the value directly).

filterSeries(array, closure)

filterSeries<T>(array: Array<T>, closure: (value: T, index: number) => Promise<boolean> | boolean): Promise<Array<T>>

Same as filter but will run all functions in series.

entangledCallback(optional_value_resolver)

entangledCallback<T>(optional_value_resolver?: (...values: Array<any>) => T): [Promise<T>, (err: ?Error, ...values: Array<any>) => void]

Returns a tuple where the first item is a promise and the second item is a node-style callback function. The two are connected so that when the callback is invoked, the promise will be resolved.

An optional function can be provided to reduce arguments passed to the callback to a single value.

// Example where a function that takes a callback is used:
import { readFile } from 'fs';

// Example function that calls readFile and pass the entangled callback instead of wrapping the call in new Promise(...)
function myReadFile(filename, options) {
  const [promise, callback] = promise_flow.entangledCallback();
  readFile(filename, options, callback);
  return promise;
}

// myReadFile will return a promise that is either resolved with the file content or rejected with the error from readFile.
myReadFile('./file.txt').then(handleFileData, handleReadError);
// Example callback invocation and the equivalent state of the entangled promise:
const [promise, callback] = promise_flow.entangledCallback();
// callback() is equivalent to Promise.resolve()
// callback(null, 'value') is equivalent to Promise.resolve('value')
// callback(null, 'a', 'b', 'c') is equivalent to Promise.resolve('a')
// callback(new Error('error')) is equivalent to Promise.reject(new Error('error'))

// The optional function argument is used to reduce values passed to the callback to a single value
const [promise, callback] = promise_flow.entangledCallback(Array.of);
// callback(null, 'a') is equivalent to Promise.resolve(['a'])
// callback(null, 'a', 'b', 'c') is equivalent to Promise.resolve(['a', 'b', 'c'])