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

@liqd-js/timed-promise

v1.1.0

Published

Chainable Promises with timeouts

Downloads

14

Readme

TimedPromise: ECMAScript 6+ class extending standard JavaScript Promises with timeout functionality

NPM version Build Status Coverage Status NPM downloads Known Vulnerabilities MIT License

Sneak Peek

This library extends standard ECMAScript Promises with the timeout functionality and works in any browser supporting Promises or Node.js environment.

  1. Converting standard Fetch Promise to TimedPromise with the 3s timeout
let data = await new TimedPromise( fetch('http://example.com/movies.json') )
	.then( response => response.json() )
	.then( json => { return { ok: true, json } })
	.timeout( 3000, { ok: false, error: 'fetch-timeouted' })
	.catch( error => { return { ok: false, error } });
  1. Creating new TimedPromise with the 3s timeout
let data = await new TimedPromise( ( resolve, reject, timeout ) =>
{
  console.log( `Fetch timeout set to ${timeout}ms` );

  fetch('http://example.com/movies.json')
    .then( response => response.json() )
    .then( json => resolve({ ok: true, json }) )
    .catch( error => reject({ ok: false, error }) );
})
.timeout( 3000, { ok: false, error: 'fetch-timeouted' })
.catch( error => error );

Motivation

Standard JavaScript Promises does not provide any support for timeouts - failing the Promise once the timer is elapsed. There are many solutions how to simulate such functionality - for example creating new Promise with the reject callback triggered by the setTimeout function or using Promise.race() method for the original Promise and a new Promise resolved by the setTimeout function.

Table of Contents

Installing

npm i @liqd-js/timed-promise

or include script for a browser release

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/radixxko/liqd-timed-promise/dist/timed_promise.min.js"></script>

Class: TimedPromise extends Promise

new TimedPromise( executor )

  • executor {Function|Promise}

TimedPromise.timeout( timeout_ms[, reason = 'timeout'[, catchable_by_parent = false]] )

  • timeout_ms {Number} Timeout for the Promise in miliseconds
  • reason {Any} Exception thrown by the Promise when it is not resolved within a timeout
    • defaults to {String} 'timeout'
  • catchable_by_parent {Boolean} Determines whether a timeout exception can be catched and resolved by a preceding catch in the Promise chain

Returns this.

TimedPromise.created()

Returns a number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the TimedPromise creation.

TimedPromise.elapsed()

Returns a number representing the milliseconds elapsed since the TimedPromise creation.

TimedPromise.remaining()

Returns a number representing the milliseconds remaining until the TimedPromise timeout.

TimedPromise.then( onFulfilled[, onRejected] )

Returns new TimedPromise

p.then( onFulfilled[, onRejected] );

p.then( ( value, remaining_ms ) =>
{
	// fulfillment
}, ( reason, remaining_ms ) =>
{
	// rejection
});

TimedPromise.catch( onRejected] )

Returns new TimedPromise

p.catch( onRejected );

p.catch( ( reason, remaining_ms ) =>
{
	// rejection
});