Giter Club home page Giter Club logo

through2-promise's Introduction

through2-promise

A small promise-based wrapper for through2, based on RangerMauve's through2-map-promise

Quickstart

npm install --save through2-promise

Some examples:

var through2 = require("through2-promise");
fs.createReadStream('ex.txt')
.pipe(through2(function (chunk) {
    for (var i = 0; i < chunk.length; i++) {
        if (chunk[i] == 97)
            chunk[i] = 122; // swap 'a' for 'z'

        this.push(chunk);
    }
}))
.pipe(fs.createWriteStream('out.txt'))
.on('finish', function () {
    doSomethingSpecial()
});

Or object streams:

var all = []

fs.createReadStream('data.csv')
.pipe(csv2())
.pipe(through2.obj(function (chunk) {
    var data = {
        name    : chunk[0],
        address : chunk[3],
        phone   : chunk[10]
    };
    this.push(data);
}))
.on('data', function (data) {
    all.push(data)
})
.on('end', function () {
    doSomethingSpecial(all)
});

API

through2-promise([options,] [fn] [, flushFunction])

Creates a transform stream which calls your transforming function, fn. You can throw within your function to automatically reject the promise and error-out the stream. options is the optional object that gets passed into through2.

this is the through2 stream, so you can do this.push() just like in through2. If you return a Promise, and the Promise resolves to a value, this value will be written to the stream (just like passing a value to the callback in through2).

flushFunction() is a function that runs after the source stream has been consumed. It has access to this.push(), and also any value returned will be appended to the written results.

through2-promise.obj([options,] [fn] [, flushFunction])

Same as the former, but the stream is created in objectMode.

through2-promise.ctor([options,] [fn] [, flushFunction])

Creates a constructor for your transform stream in case you want to be more efficient.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.