Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

avait

Package Overview
Dependencies
Maintainers
0
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

avait

Async error handling and fetch without try-catch.

  • 2.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

avait

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.

Keywords

FAQs

Package last updated on 04 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc