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

@art-of-coding/async-util

v0.0.1

Published

A set of async utility helpers

Downloads

1

Readme

WS

Three simple, asynchronous methods that may make life easier.

All methods return a Promise.

Methods

wait (ms: number)

Waits for ms milliseconds before resolving. This method encapsulates setTimeout into a Promise.

import { wait } from './index'

async function run () {
  console.log('before')
  await wait(1000) // wait 1000ms or 1s
  console.log('after')
}

waitUntil (event: string, emitter: EventEmitter)

Waits until event is emitted on the given emitter. The promise is resolved with an array containing the event's arguments, or an empty array if there were none.

import { EventEmitter } from 'events'
import { waitUntil } from './index'

const emitter = new EventEmitter()

async function run () {
  // some code to emit an event...
  setTimeout(() => emitter.emit('some-event'), 1000)

  console.log('before')
  await waitUntil('some-event', emitter)
  console.log('after')
}

waitOrTimeout (event: string, emitter: EventEmitter, ms: number)

Waits until event is emitted on the given emitter, or when ms has elapsed. If the time has elapsed, the Promise rejects.

import { EventEmitter } from 'events'
import { waitOrTimeout } from './index'

const emitter = new EventEmitter()

async function run () {
  // some code to emit an event...
  setTimeout(() => emitter.emit('some-event'), 1500)

  console.log('before')
  await waitOrTimeout('some-event', emitter, 1000)
  console.log('after')
}

waitOrError (event: string, emitter: EventEmitter, errorEvent = 'error')

Waits until either event or errorEvent is emitted on the given emitter. If errorEvent is emitted, the Promise rejects.

import { EventEmitter } from 'events'
import { waitErError } from './index'

const emitter = new EventEmitter()

async function run () {
  // some code to emit an event...
  setTimeout(() => emitter.emit('some-event'), 1000)
  // some code to emit an error...
  // setTimeout(() => emitter.emit('error', new Error('some error message')), 1000)

  console.log('before')
  await waitOrError('some-event', emitter)
  console.log('after')
}

resolveOrReject (resolveEvents: string[], rejectEvents: string[], emitter: EventEmitter)

Resolves or rejects based on which event is received.

import { EventEmitter } from 'events'
import { resolveOrReject } from './index'

const emitter = new EventEmitter()

async function run () {
  const resolveEvents = [ 'connected', 'ready' ]
  const rejectEvents = [ 'error', 'close' ]

  // some code to emit an event...
  setTimeout(() => emitter.emit('ready'), 1000)

  console.log('before')
  await resolveOrReject (resolveEvents, rejectEvents, emitter)
  console.log('after')
}

waitFor (resolveEvents: string[], rejectEvents: string[], timeout: number, emitter: EventEmitter)

Resolves or rejects based on which event is received, or until the optional timeout has expired. The promise is also cancelable.

import { EventEmitter } from 'events'
import { waitFor } from './index'

const emitter = new EventEmitter()

async function run () {
  const resolveEvents = [ 'connected', 'ready' ]
  const rejectEvents = [ 'error', 'close' ]
  const timeout = 10 * 1000 // optional timeout 10s

  // some code to emit an event...
  setTimeout(() => emitter.emit('ready'), 1000)

  const { promise, cancel } = waitFor (resolveEvents, rejectEvents, timeout, emitter)

  console.log('before')
  await promise
  console.log('after')
}

License

Copyright 2017 Art of Coding.

This software is licensed under the MIT License.