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

@tioniq/unawaited

v1.0.1

Published

Utility to fire-and-forget a promise

Downloads

109

Readme

Unawaited

Unawaited is a utility that allows you to call promises without awaiting them, ensuring that errors are suppressed and do not cause unhandled promise rejections. This can be helpful for fire-and-forget operations where you don't need to handle the promise's outcome.

Installation

Install the package via npm:

npm install @tioniq/unawaited

Table of Contents

Usage

Basic Usage

When using unawaited, you don't need to await the promise, and it will suppress any errors that occur during the execution

import { unawaited } from '@tioniq/unawaited'

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    reject(new Error('Hello, World!'))
  }, 1000)
})

unawaited(promise) // No need to await it, no error will be thrown

Function Usage

You can also pass a function that returns a promise, and unawaited will call the function and ignore any rejections:

import { unawaited } from '@tioniq/unawaited'

unawaited(() => Promise.reject(new Error("An error occurred")))
// The promise will reject, but no error will be thrown

unawaited(() => {
  throw new Error("An error occurred")
})
// Or even throw an error directly

Error Handling

If you want to handle errors that occur during the promise execution, you can pass a second argument to the unawaited function:

import { unawaited } from '@tioniq/unawaited'

const promise = new Promise((resolve, reject) => {
  reject(new Error('Hello, World!'))
})

unawaited(promise, (error) => {
  console.error(error.message) // Prints "Hello, World!"
})

General Error Handling

If you want to handle all errors that occur during the promise execution on global level, you can set the unawaited.exceptionHandler property. It will be called with the error and the source object (if provided):

import { unawaited } from '@tioniq/unawaited'

// Set global exception handler
unawaited.exceptionHandler = (error, source) => {
  console.error(`Unhandled exception "${error.message}" in source ${source}`)
}

// Some source object, for example the string. Can be omitted.
const source = "hello"

unawaited(() => throw new Error("Hello, World!"), source) // Prints "Unhandled exception "Hello, World!" in source hello"

By default, the unawaited.exceptionHandler is set to noop.

Handle Unknown Argument

If unawaited is called with an argument that is not a promise or a function, it will call the exception handler with an error:

import { unawaited } from '@tioniq/unawaited'

unawaited.unknownValueHandler = (value) => {
  console.error(`Unknown value "${value}"`)
}

unawaited('Hello, World!') // Prints "Unknown value "Hello, World!"

By default, the unawaited.unknownValueHandler is set to noop.

Bad Usage

import { unawaited } from '@tioniq/unawaited'

async function badFunction() {
  if (true) {
    throw new Error('Hello, World!')
  }
  // Do something
}

unawaited(badFunction()) // Will throw an error, because the function execution is not wrapped in a try-catch block

// Do instead
unawaited(badFunction) // Will call the function and handle the error
// Or
unawaited(() => badFunction()) // Will call the function and handle the error

This pattern is especially useful for handling one-off operations where you don't need to wait for the result.

License

This project is licensed under the MIT License.