Socket
Book a DemoInstallSign in
Socket

simple-await

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-await

Executing async function with simple error handling. Returns object with data and error.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

simple-await handwritten logo

simple-await

Execute async function with simple error handling. Returns object with data and error.

Installation

yarn add simple-await

Usage

import wait from 'simple-await';

function rejecter() {
    return Promise.reject(new Error("my new error"))
}

function solver(): Promise<{ name: string }> {
    return Promise.resolve({ name: "John" })
}

async function withFetch() {
    const { data, error } = await wait<object, string>(fetch('http://httpstat.us/503'));

    console.log(data, error) // undefined,  Failed to fetch 
}

async function solve() {
    const { data, error } = await wait<{ name: string }, string>(solver());

    console.log(data, error) // { name: "John" }, undefined
}

async function reject() {
    const { data: result, error: cause } = await wait(rejecter());

    console.log(result, cause) // undefined, my new error
}

Note

Simple-await uses Promise.allSettled() under the hood. Please check MDN for browser compatibility. Also, note that tsconfig configured to target es2020. Please let me know if you need to change it or support some older broswer versions.

License

MIT

Keywords

async

FAQs

Package last updated on 17 Apr 2023

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