delayable-idle-abort-promise
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

NPM npm npm

delayable-idle-abort-promise

Race an array of promises against a promise that rejects if nothing happens in the specified time window, but that can be postponed by signaling activity

Example

import DelayAbort, { AbortError } from 'delayable-idle-abort-promise'
 
const onePromise = (abort) => new Promise(() => {
  setTimeout(abort, Math.round(Math.random() * 1000))
})
 
const control = DelayAbort(1000)
 
setInterval(() => {
  // postpone() will delay the "natural" internal interval,
  // but not calls to abort()
  control.postpone()
}, 100)
 
try {
  const result = await control.race([
    onePromise(control.abort),
    anotherPromise
  ])
 
  // do something with result
} catch (e) {
  if (instanceof AbortError) {
    console.log(e.lastActivity, e.asDate())
  }
}

License

MIT

/delayable-idle-abort-promise/

    Package Sidebar

    Install

    npm i delayable-idle-abort-promise

    Weekly Downloads

    4

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    7.28 kB

    Total Files

    7

    Last publish

    Collaborators

    • pocesar