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

v2.1.1

Published

Async error handling and fetch without try-catch.

Downloads

87

Readme

avait

Async error handling and fetch 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' or similar.

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])

Simple fetch

This is a super small wrapper around fetch that's supposed to make error handling and accessing the data simple.

import { load } from 'avait'

const { error, status, data, text } = await load('http://localhost:3000/api') // GET method

// error: boolean | string, indicating if the request errored.
// status: The HTTP status code.
// data: parsed JSON data if response is JSON.
// text: string, text content if response contains text.
// ...props: For a JSON response top-level object properties will be spread on the return object.


await load('http://localhost:3000/api', { name: 'John Doe' }) // POST method
await load('http://localhost:3000/api', { id: 1, name: 'John Doe' }) // PUT method
await load('http://localhost:3000/api', 1) // DELETE method

Converting an Async Method to a Synchronous One

[!IMPORTANT] This feature has been removed with Version 2 of this plugin. I haven't found it useful and it currently doensn't work with Bun. Check out make-synchronized if you're looking to provide a synchronized interface to an asynchronous method.

Credits

Error handling inspired by await-to-js.