@mainframe/utils-time
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

utils-time

Time-related utilities.

Installation

yarn add @mainframe/utils-time

Usage

import { createWithTimeout, TimeoutError, withTimeout } from 'utils-id'

const max10secs = createWithTimeout(10000)

const run = async () => {
  try {
    const tooLong = await max10secs(fetch(...))
  } catch (err) {
    if (err instanceof TimeoutError) {
      ...
    }
  }

  const longerTime = await withTimeout(fetch(...), 60 * 1000) // 1 min
}

API

sleep

Waits for the given time before resolving.

Arguments

  1. time: number

Returns Promise<void>

new TimeoutError

Arguments

  1. message: string

withTimeout

Races between the given wait promise and time before rejecting with a TimeoutError.

Arguments

  1. wait: Promise<T>
  2. time: number

Returns Promise<T>

createWithTimeout

Creates a withTimeout() function with a given default time.

Arguments

  1. time: number

Returns (wait: Promise<T>, time?: number) => Promise<T>

createGetTime

Creates function that will return an always increasing current time value based on Date.now().

Returns () => number

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i @mainframe/utils-time

Weekly Downloads

1

Version

0.2.0

License

MIT

Unpacked Size

5.42 kB

Total Files

6

Last publish

Collaborators

  • aclarke
  • diogoperillo
  • howleysv
  • mosic
  • paul_lecam
  • sarahwiley