New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dirkluijk/promise-stream

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dirkluijk/promise-stream

A stream-like Promise for JavaScript

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

PromiseStream 🌊

A stream-like promise that emits multiple values over time

NPM version NPM downloads Build status

Overview

What? 🤔

A simple building block that behaves like a regular Promise<T>, but can emit multiple values over time.

  • Compatible with async/await (awaits completion)
  • Provides built-in async iterator
  • Contains configurable buffer size

Limitations & when to use? 🤷‍♂️

Like regular promises, a PromiseStream<T> is not cancellable, is hot, shared, and will replay the last value. Furthermore, no built-in operator support (like map or flatMap) is currently provided.

The limitations are intended. For extensive reactive apps I would recommend to use RxJS instead.

Roadmap ideas 💡

  • Provide built-in RxJS compatibility utils
  • Support different backpressure mechanisms

How to use 🌩

Install

npm install @dirkluijk/promise-stream

Creating a PromiseStream<T>

This works the same as a regular Promise, but you have three callbacks: next, complete and error.

import { PromiseStream } from '@dirkluijk/promise-stream';

const myStream = new PromiseStream<string>((next, complete, error) => {
    next('foo');
    next('bar');
    next('baz');
    complete();
})
  • next always expects a value
  • complete never expects a value
  • error accepts an optional error value
  • once completed or failed, no values are accepted anymore

Consuming a PromiseStream<T>

You can use callbacks:

myStream
    .iterate(value => {
        // executed when value is emitted
    })
    .then(() => {
        // executed when completed
    })
    .catch((error) => {
        // executed when failed
    })

Since a PromiseStream invokes the .then() callback upon completion, it is compatible with async/await:

try {
    await myStream.iterate(value => {
        // executed when value is emitted
    });
} catch (error) {
    // executed when failed
}

// executed when completed

Additionally, you can also use the async iterator:

try {
    for await (const value of myStream.asyncIterator()) {
        // executed when value is emitted
    };
} catch (error) {
    // executed when failed
}

// executed when completed

FAQs

Package last updated on 22 Sep 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc