Skip to content

jcoreio/web-streams-finally

Repository files navigation

@jcoreio/web-streams-finally

userland implementation of proposed finally() API for web streams

CircleCI Coverage Status semantic-release npm version

ReadableStreamWithFinally

import { ReadableStreamWithFinally } from '@jcoreio/web-streams-finally'

const stream = new ReadableStreamWithFinally({
  async pull(controller) {
    ...
  },
  async finally(why: 'close' | 'cancel' | 'error', reason?: any) {
    // this will be called when the stream is closed, canceled, or errored
  }
})

ReadableStreamWithSignal

import { ReadableStreamWithSignal } from '@jcoreio/web-streams-finally'

const stream = new ReadableStreamWithSignal({
  async pull(controller) {
    await doSomething({
      signal: controller.signal, // will be aborted when stream errors is closed, canceled, or errored
    })
    ...
  },
  async finally(why: 'close' | 'cancel' | 'error', reason?: any) {
    // this will be called when the stream is closed, canceled, or errored
  }
})