polled
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Polled

Single-shot request + polling combined into a single AsyncGenerator

Use case

Long-running HTTP request where the backend uploads intermediate results to a pollable endpoint

  1. Client: GET /server
  2. Server: Starts processing...
    • For each result: PUT /endpoint
  3. Client: polls to GET /endpoint/poll
  4. Server returns (HTTP response)

Usage

import { polled } from 'polled'

async function* polledRequest() {
  const { values, response } = polled(
    fetch('/request'),
    () => fetch('/endpoint/poll'),
    2 // delaySecs
  )
  for await (const pollResult of values)
      // do something with polled result
      
  // do something with response
  await response
}

/polled/

    Package Sidebar

    Install

    npm i polled

    Weekly Downloads

    0

    Version

    1.0.2

    License

    ISC

    Unpacked Size

    15.9 kB

    Total Files

    9

    Last publish

    Collaborators

    • moveread