Function time

  • Utility for defining milliseconds

    Type Parameters

    • Amount extends number

    Parameters

    Returns Time<Amount>

    Example

    import { time } from "@rjweb/utils"

    time(10).h() // 36000000

    Since

    1.5.0

Methods

Methods

  • See how long a function takes to run

    Type Parameters

    • Fn extends (() => any)

    Parameters

    Returns Fn extends (() => Promise<R>)
        ? Promise<[number, Awaited<R>]>
        : Fn extends (() => R)
            ? [number, R]
            : never

    Example

    import { time } from "@rjweb/utils"

    const [ time, result ] = time.fn(async() => {
    await time.wait(time(20).s())
    })

    console.log(time, result) // 20000 undefined

    Since

    1.5.3

    Supports

    nodejs, browser

  • Parse a time string into milliseconds

    Parameters

    • input: string

    Returns null | number

    Example

    import { time } from "@rjweb/utils"

    time.parse('2s') // 2000
    time.parse('2m 30 s') // 150000
    time.parse('2h 30minute 30s') // 9030000
    time.parse('2day 3minute 30h 30s 4 ms') // 183810004
    time.parse('Jan 2 2022') // <will depend on current date>
    time.parse('1w -1d') // 518400000
    time.parse('1d -12h') // 43200000
    time.parse('hi') // null

    Since

    1.12.8

    Supports

    nodejs, browser

  • Wait asyncronously for x ms

    Parameters

    • ms: number

    Returns Promise<void>

    Example

    import { time } from "@rjweb/utils"

    console.log('hi!')
    await time.wait(time(2).s())
    console.log('2 sec later')

    Since

    1.5.0

    Supports

    nodejs, browser