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

promise.timeout

v1.2.0

Published

add timeout support for async function

Downloads

1,642

Readme

promise.timeout

add timeout support for async function

Build Status Coverage Status npm version npm downloads npm license

Install

$ pnpm add promise.timeout

Note

  • this module targets ES5 environment.
  • this module require global AbortController, Node.js has builtin global since v15

API

var ptimeout = require('promise.timeout')

ptimeout(fn, timeout)

  • fn the async function
  • timeout in ms
var ptimeout = require('promise.timeout')

// a function will cost 20ms
function test() {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve(20)
    }, 20)
  })
}

var test10 = ptimeout(test, 10)
var test50 = ptimeout(test, 50)

// 10 timeout
try {
  await test10()
} catch (e) {
  e.should.be.ok()
  e.should.be.instanceof(ptimeout.TimeoutError)
  e.message.should.match(/timeout/)
  e.timeout.should.equal(10)
}

// 50 ok
var _50 = await test50()
_50.should.be.ok()
_50.should.equal(20)

singal

ptimeout will provide an extra runtime argument signal: AbortSignal, you can use the signal to register abort action. when timeout reached, the abort action will be executed

var ptimeout = require('promise.timeout')

// a function will cost 20ms
function test(signal) {
  return new Promise(function (resolve, reject) {
    var timer = setTimeout(function () {
      resolve(20)
    }, 20)

    // clean up
    signal.addEventListener('abort', () => {
      clearTimeout(timer)
    })
  })
}

var test10 = ptimeout(test, 10)
try {
  await test10()
} catch (e) {
  e.should.ok()
}

FAQ

Q: why move to AbortController

A:

  • easy to type, easy to strip signal argument, easy to use with TypeScript
  • AND it's shiped in Node.js https://nodejs.org/api/globals.html#class-abortcontroller
  • for browser, users should consider a polyfill for AbortController & AbortSignal if not provided nativly

Q: Why onCancel

A: Think onCancel like the AbortController

with AbortController you need to

function normalFn(a, r, g, s, controller: AbortController) {
  controller.signal.addEventListener('abort', () => {
    // cancel operations that starts in `normalFn` body
  })
}
  • and ptimeout will call the controller.abort() if any timeout exceeds
  • and with onCancel, you provide a cancel operation to ptimeout, ptimeout will call that

That's the same, and I don't want to depend on an extra package abort-controller

See Also

Changelog

CHANGELOG.md

License

the MIT License http://magicdawn.mit-license.org