0.0.2 • Published 5 years ago

maxwait v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

maxwait

A Node package for timing functions max execution time and timeout once the specified time is reached.

Usage

    const MaxWait = require('maxwait').defualt
    // or
    import MaxWait from 'maxwait'
    // https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
    const controller = new AbortController()
    const signal = controller.signal
    // let's create a MaxWait instance with max wait of 5000 millisecons
    const maxWait = new MaxWait(5000)
    // register a handler to be called if timeout was reached
    maxWait.onTimeout(()=>{
      controller.abort()
    })
    // start the count down
   maxWait.start()
   fetch('/some-endpoint', {
      method: 'get',
      signal: signal,
    }).then(response=>{
      // the request was successful so you need to make sure
      // you free up some memory by called the `done` method
      maxWait.done()
    }).catch(error=>{
      // also call done here too.
      maxWait.done()
    })