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

bask-promise

v1.0.9

Published

Set of common promise utilities, not available in basic node.js features * Delay promise * Delay before or after promise * Run promises in sequence, e.g. requests to database * Repeated failed promises

Downloads

177

Readme

BASK Promise

Set of common promise utilities, not available in basic node.js features

  • Delay promise
  • Delay before or after promise
  • Run promises in sequence, e.g. requests to database
  • Repeated failed promises

Install

npm i bask-promise --save

Then import module in code:

const promises = require('bask-promise');
promises.delay(100);

Or

const {delay} = require('bask-promise');
delay(100);

Delay

Simple delay

const {delay} = require('bask-promise');
delay(1000).then(() => console.log('Run after 1 second'));

Delay after promise

const {delayAfter} = require('bask-promise');
delayAfter(Promise.resolve('1 second delay after'), 1000);

Delay before promise

const {delayBefore} = require('bask-promise');
delayBefore(() => Promise.resolve('1 second delay before'), 1000);

Random delay

Random time period delay

const {random} = require('bask-promise');
random(1000).then(() => console.log('Run after 0...1 second'));

Specify lower bound

const {random} = require('bask-promise');
random(2000, 1000).then(() => console.log('Run after 1...2 seconds'));

Random delay after promise

const {randomAfter} = require('bask-promise');
randomAfter(Promise.resolve('0...1 second delay after'), 1000);
randomAfter(Promise.resolve('0...2 second delay after'), 2000, 1000);

Random delay before promise

const {randomBefore} = require('bask-promise');
randomBefore(() => Promise.resolve('0...1 second delay before'), 1000);
randomBefore(() => Promise.resolve('1...2 second delay before'), 2000, 1000);

Delay callback

If you want to use delay as a callback inside then, you can use delayFun(ms)

const {delayFun} = require('bask-promise');
Promise.resolve("Delay for 1s in then after promise").then(delayFun(1000));

Sequence

Node.js have basic Promise.all function to run promises in parallel, but to run sequence you have to implement it yourself. This is a typical task for example when quering database or external services.

const {sequence} = require('bask-promise');
sequence([
        () => Promise.resolve('Query database 1'),
        () => Promise.resolve('Query database 2'),
    ])

If you have the same query function, but different arguments - use keySequence:

const {keySequence} = require('bask-promise');
keySequence([1,2,3,4,5], key => queryDatabasePromise(key));

Repeat

Sometimes you need to ensure request that is properly executed. But due to network connections, requests can fail. In that case you may want to repeat failed requests automatically.

Repeat up to 3 times (4 calls total):

const {repeat} = require('bask-promise');
repeat(() => repeatThisPromiseIfFails(), 3);

Repeat until success:

repeat(() => repeatThisPromiseIfFails(), -1);

Log every error

repeat(() => repeatThisPromiseIfFails(), 3, error => console.error(error));

Delay after error

const {repeat, delay} = require('bask-promise');
repeat(() => repeatThisPromiseIfFails(), 3, error => delay(1000));

Promise resolve from outside

If you need to create a promise instance, pass it somewhere, and resolve it later from outside, you can use deferred function

const {deferred} = require('bask-promise');
const promise = deferred();

//use or pass promise somewhere
promise.then(() => null).catch(() => null);

//then resolve promise
promise.resolve(42);
//or
promise.reject(new Error("foo"));