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

avait

v1.0.0

Published

Async error handling without try-catch.

Downloads

201

Readme

avait

Async error handling without try-catch.

Usage

import { it } from 'avait'
import { readFile } from 'fs/promises'

const { error, value } = await it(readFile('./my-file.txt', 'utf-8'))

if (error) return alert('Error')
console.log(`File contents: ${value}`)

It's possible to resolve multiple promises in a row.

const { error, title } = await it(fetch('https://dummyjson.com/products/1')).add((next) =>
  next.json(),
)
// title => 'iPhone 9'

Error Handler

When an error is thrown but the error property isn't accessed errors will be sent to any registered error handlers.

import { it, registerErrorHandler } from 'avait'
import { readFile } from 'fs/promises'

registerErrorHandler((error) => alert(error))

const { value } = await it(readFile('./my-file.txt', 'utf-8'))

console.log(`File contents: ${value}`)

Multiple Promises

It's possible to pass an array of promises. In this case the result value as well as the error will also be returned as an array. Using the second argument parallelism can be enabled which leads to the promises being run in parallel.

import { it } from 'avait'

const { value } = await it([firstPromise, secondPromise])
console.log(value[0])
// With parallelism enabled
const { value } = await it([firstPromise, secondPromise], { parallel: true })
console.log(value[1])

Converting an Async Method to a Synchronous One

Using the toSync method it's possible to leverage node worker_threads to turn an async method into a synchronous one. This is usually not necessary nor recommended as asynchronous methods are supported in any environment nowadays. As the first argument the method accepts a module path or a file (basically anything that can be passed to import) with a specific export as the second argument which will default to the default export. The second argument can be an array in case multiple calls should be chained. Proper chaining is important as the final result needs to be serializable in order to be passed back from the worker. The function returned by toSync can then be synchronously be called adding any arguments as an array or in the case of chaining multiple arrays.

import { toSync } from 'avait/synchronize'

// Synchronize an async module.
const fileContents = toSync('fs/promises', 'readFile')('./my-file.txt', 'utf-8')
const { title } = toSync('node-fetch', ['default', 'json'])([
  ['https://dummyjson.com/products/1'],
  [],
])
const { id, description } = toSync('axios', ['get', 'data'])([['https://dummyjson.com/products/1']])

// Synchronize an async method from a local file.
const prased = toSync('../parse-data.js', 'parse')([[data]])

Credits

Error handling inspired by await-to-js.

Async to sync approach taken from @prettier/sync.