![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
stream-to-promise
Advanced tools
The 'stream-to-promise' npm package is a utility that converts a stream into a promise. This is particularly useful when working with Node.js streams and you need to handle the completion of a stream operation using async/await or promise-based syntax.
Convert Stream to Promise
This feature allows you to convert a readable stream into a promise. The promise resolves when the stream ends, making it easier to handle stream completion in an async/await context.
const streamToPromise = require('stream-to-promise');
const fs = require('fs');
async function readFile() {
const readStream = fs.createReadStream('example.txt');
await streamToPromise(readStream);
console.log('Stream has ended');
}
readFile();
Handle Stream Errors
This feature demonstrates how to handle errors that occur during the stream operation. The promise will reject if the stream emits an 'error' event, allowing you to catch and handle the error using try/catch.
const streamToPromise = require('stream-to-promise');
const fs = require('fs');
async function readFile() {
const readStream = fs.createReadStream('nonexistent.txt');
try {
await streamToPromise(readStream);
} catch (error) {
console.error('Stream error:', error);
}
}
readFile();
'stream-to-array' is a package that collects all data from a stream into an array and returns a promise that resolves with the array. It is useful when you need to gather all chunks of data from a stream and process them together. Unlike 'stream-to-promise', which resolves when the stream ends, 'stream-to-array' also provides the collected data.
'get-stream' is a package that converts a stream into a string, buffer, or array, and returns a promise that resolves with the collected data. It offers more flexibility in terms of the output format compared to 'stream-to-promise', which only resolves when the stream ends without providing the data.
'promise-streams' is a package that provides a set of utilities for working with streams using promises. It includes functions for collecting stream data, piping streams, and more. It offers a broader range of functionalities compared to 'stream-to-promise', which focuses solely on converting a stream to a promise.
Convert streams (readable or writable) to promises
npm install --save stream-to-promise
streamToPromise(readableStream).then(function (buffer) {
// buffer.length === 3
})
readableStream.emit('data', new Buffer())
readableStream.emit('data', new Buffer())
readableStream.emit('data', new Buffer())
readableStream.emit('end') // promise is resolved here
streamToPromise(writableStream).then(function () {
// resolves undefined
})
writableStream.write('data')
writableStream.end() // promise is resolved here
const err = new Error()
streamToPromise(stream).catch(function (error) {
// error === err
})
stream.emit('error', err) // promise is rejected here
FAQs
Convert streams (readable or writable) to promises
The npm package stream-to-promise receives a total of 845,905 weekly downloads. As such, stream-to-promise popularity was classified as popular.
We found that stream-to-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.