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

promised-pipe

v1.1.1

Published

Simple async pipe function to mix pure and promise-based functions easily

Downloads

348

Readme

promised-pipe

npm version Build Status

A sweet composition of ramda.pipe and q.promised to make async pipes simple. No dependencies. Extremely lightweight.

Requirements

You only have to have Node 4+ or a ES5-compatible browser

Introduction

This library is extremely useful for those who use functions pipes (ramda.pipe, inverted underscore.compose, etc.), but struggle with composing both synchronous and asynchronous (Promise returning) functions in the same pipeline.

Install

Run:

npm i --save promised-pipe

Include:

const pipe = require('promised-pipe')
// or
import pipe from 'promised-pipe'

Usage

It is a function with the same API as ramda.pipe, except the created function will always return a Promise

const resultedFn = pipe(fn1, fn2, fn3)
resultedFn(args) // => Promise

There is a bunch of tests in test.js which are quite readable

Examples

You can play and run all of these examples by npm i ramda && node example.js (Node 6+ required).

Basic example with some simple math:

const inc = x => x + 1 // synchronous function
const mul3 = x => x * 3 // synchronous function
const div2promise = x => Promise.resolve(x / 2) // asynchronous function

const mathFn = pipe(
    inc,
    div2promise,
    mul3
)

mathFn(5).then(console.log) // 5 + 1 / 2 * 3 = 9

Numbers aren't so interesting! Example with fs:

const filePath = '/tmp/promised_pipe_example'
// "Promisify" fs modules
const readFile = path => new Promise((res, rej) => fs.readFile(path, (err, data) => err ? rej(err) : res(data)))
const writeFile = (path, data) => new Promise((res, rej) => fs.writeFile(path, data, (err) => err ? rej(err) : res(path)))

const fsFn = pipe(
    text => writeFile(filePath, text),
    path => readFile(path),
    text => text.toString(),
    text => text.toUpperCase(),
    text => writeFile(filePath, text).then(() => text)
)

fsFn('hello').then(console.log) // HELLO

Still isn't sexy, right? Let's add some Ramda to the previous example:

const R = require('ramda')
const readFileC = R.curry(readFile)
const writeFileC = R.curry(writeFile)
const writeToTmp = writeFileC(filePath + '_ramda')

const ramdaFsFn = pipe(
    writeToTmp,
    readFileC,
    R.toString,
    R.toUpper,
    R.tap(writeToTmp)
)

ramdaFsFn('hello, ramda').then(console.log) // HELLO, RAMDA

So you can easily mix functions which return non-promises and functions which return promises.

It is highly recommended to use it with Ramda since it will make your experience a lot better.

More on the topic