Skip to content

Combines an array of AbortSignals into a single signal that is aborted when any signal is

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

jacobheun/any-signal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

any-signal

codecov CI

Combines an array of AbortSignals into a single signal that is aborted when any signal is

About

Similar to AbortSignal.any except the returned promise has a .clear method that removes all event listeners added to passed signals preventing memory leaks.

At the time of writing at least, AbortSignal.any leaks memory in Node.js and Deno environments:

Example

import { anySignal } from 'any-signal'

const userController = new AbortController()

// Abort after 1 second
const timeoutSignal = AbortSignal.timeout(1000)

const combinedSignal = anySignal([userController.signal, timeoutSignal])
combinedSignal.addEventListener('abort', () => console.log('Abort!'))

try {
  // The user or the timeout can now abort the action
  await performSomeAction({ signal: combinedSignal })
} finally {
  // Clear will clean up internal event handlers
  combinedSignal.clear()
}

Install

$ npm i any-signal

Browser <script> tag

Loading this module through a script tag will make its exports available as AnySignal in the global namespace.

<script src="https://unpkg.com/any-signal/dist/index.min.js"></script>

Acknowledgements

The anySignal function is taken from a comment by jakearchibald

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Combines an array of AbortSignals into a single signal that is aborted when any signal is

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Contributors 7