Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including promise-duplex with all npm packages installed. Try it out:

var promiseDuplex = require("promise-duplex")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

promise-duplex v5.0.3

Return promise for duplex stream

promise-duplex

Build Status Coverage Status npm

This module allows to convert Duplex stream into its promisified version, which returns Promise object fulfilled when stream's events occurred.

The module combines promise-readable and promise-writable in one.

Requirements

This module requires Node >= 6.

Installation

npm install promise-duplex

Usage

const {PromiseDuplex} = require("promise-duplex")

Typescript:

import PromiseDuplex from "promise-duplex"
// or
import {PromiseDuplex} from "promise-duplex"

constructor

const promiseDuplex = new PromiseDuplex(stream)

PromiseDuplex object requires Duplex object to work.

Example:

const net = require("net")
const {PromiseDuplex} = require("promise-duplex")

const stream = new net.Socket()

const promiseDuplex = new PromiseDuplex(stream)

Typescript:

import net from "net"
import PromiseDuplex from "promise-duplex"

const stream = new net.Socket()

const promiseDuplex = new PromiseDuplex(stream)

stream

const stream = promiseDuplex.stream

Original stream object.

Example:

console.log(promiseDuplex.stream.localAddress)

read

const chunk = await promiseDuplex.read(chunkSize)

Check PromiseReadable.read for details.

readAll

const content = await promiseDuplex.readAll()

Check PromiseReadable.readAll for details.

iterate

for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}

Check PromiseReadable.iterate for details.

Symbol.asyncIterator

for await (const chunk of promiseDuplex.iterate(chunkSize)) {
}

Check PromiseReadable[Symbol.asyncIterator] for details.

setEncoding

promiseDuplex = await promiseDuplex.setEncoding(encoding)

Check PromiseReadable.setEncoding for details.

write

await promiseDuplex.write(chunk)

Check PromiseWritable.write for details.

writeAll

await promiseDuplex.writeAll(content, chunkSize)

Check PromiseWritable.writeAll for details.

end

await promiseDuplex.end()

Check PromiseWritable.once for details.

once

const result = await promiseDuplex.once(event)

Check PromiseReadable.once and PromiseWritable.once for details.

destroy

promiseDuplex.destroy()

This method calls destroy method on stream and cleans up all own handlers.

See also

PromiseReadable, PromiseWritable, PromiseSocket, PromisePiping.

License

Copyright (c) 2017-2019 Piotr Roszatycki piotr.roszatycki@gmail.com

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free