npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

moratorium

v1.1.5

Published

Delay a promise a specified amount of time

Downloads

4

Readme

node-moratorium

Delay a promise

Usage

const moratorium = require('moratorium')

;(async function () {
    const text = 'print this after 1234 ms'

    await moratorium.resolve(1234)

    console.log(text)
})()

API

moratorium.resolve(time, value)

Resolves a Promise after a specific amout of time.

Params:
time -> time to wait
value -> value returned by the Promise after the time set through time

Return: Promise

const value = await moratorium.resolve(1000, 'Vitória')

console.log(value) // Prints Vitória after 1000 ms

moratorium.reject(time, value)

Rejects a Promise after a specific amout of time.

Params:
time -> time to wait
value -> value returned by the Promise after the time set through time

Return: Promise

try {
    await moratorium.reject(1000, 'Vitória')
    // or
    await moratorium.reject(1000, new Error('Vitória'))
} catch (err) {
    console.log(err) // After the delay:
                     // In the frist case it prints Vitória,
                     // in the second the Error object
}

moratorium.span.resolve(time, span, value)

Resolves a Promise after a randomic span of time.

Params:
time -> time to wait
span -> lower and upper bound added to time (both bounds are inclusive)
value -> value returned by the Promise after the time set through time

Return: Promise

const value = await moratorium.span.resolve(1000, 500, 'Vitória')
console.log(value) // Prints Vitória after randomic time between 500 ms (inclusive) and 1500 ms (inclusive)

const value = await moratorium.span.resolve(2300, 100, 'Vitória')
console.log(value) // Prints Vitória after randomic time between 2200 ms (inclusive) and 2400 ms (inclusive)

moratorium.span.reject(time, span, value)

Rejects a Promise after a randomic span of time.

Params:
time -> time to wait
span -> lower and upper bound added to time (both bounds are inclusive)
value -> value returned by the Promise after the time set through time

Return: Promise

try {
    await moratorium.span.reject(1000, 500 'Vitória')
    // Rejects the promise after randomic time between 500 ms (inclusive) and 1500 ms (inclusive)

    await moratorium.span.reject(2300, 100, new Error('Vitória'))
    // Rejects the promise after randomic time between 2200 ms (inclusive) and 2400 ms (inclusive)
} catch (err) {
    console.log(err) // After the delay:
                     // In the frist case it prints Vitória,
                     // in the second the Error object
}

moratorium.range.resolve(from, to, value)

Resolves a Promise after a randomic ranged amout of time.

Params:
from -> lower bound (inclusive)
to -> upper bound (inclusive)
value -> value returned by the Promise after the time set through from and to

Return: Promise

const value = await moratorium.range.resolve(234, 780, 'Vitória')
console.log(value) // Prints Vitória after randomic time between 234 ms (inclusive) and 780 ms (inclusive)

const value = await moratorium.range.resolve(100, 350, 'Vitória')
console.log(value) // Prints Vitória after randomic time between 100 ms (inclusive) and 350 ms (inclusive)

moratorium.range.reject(from, to, value)

Rejects a Promise after a randomic ranged amout of time.

Params:
from -> lower bound (inclusive)
to -> upper bound (inclusive)
value -> value returned by the Promise after the time set through from and to

Return: Promise

try {
    await moratorium.range.reject(234, 780 'Vitória')
    // Rejects the promise after randomic time between 500 ms (inclusive) and 1500 ms (inclusive)

    await moratorium.range.reject(100, 350, new Error('Vitória'))
    // Rejects the promise after randomic time between 100 ms (inclusive) and 350 ms (inclusive)
} catch (err) {
    console.log(err) // After the delay:
                     // In the frist case it prints Vitória,
                     // in the second the Error object
}

Promise properties and methods

Each Promise object returned from the methods above has the following properties and methods:

promise.clear()

You can clear a delay by calling clear() method in the Promise returned by one of the methods above.

const moratorium = require('moratorium')

;(async function () {
    const text = 'print this after 500 instead of 1000'

    const delay = moratorium.resolve(1000, text)

    setTimeout(() => {
        delay.clear() // Resolve the delay promise
        // or
        delay.clear('Error reason') // Reject the delay promise
        // or
        delay.clear(new Error('Custom error')) // Reject the delay promise with a custom error
    }, 500)

    try {
        await delay
    } catch (err) {
        console.log(err) // If cleared with reason or error object
    }

    console.log(delay.value) // If cleared without arguments
})()

promise.time

It's the time that the Promise will have to wait before completion.

N.B: If the time is negative, it will be set to 0.

const moratorium = require('moratorium')

;(async function () {
    const delay = moratorium.resolve(1234)

    await delay

    console.log(delay.time) // Prints 1234
})()

promise.value

It's the value that will be returned by the Promise.

const moratorium = require('moratorium')

;(async function () {
    const delay = moratorium.resolve(1000, 'Vitória')

    await delay

    console.log(delay.value) // Prints Vitória
})()