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

deasyncify

v0.0.4

Published

A powerful library to handle asynchronous programming in javascript

Downloads

609

Readme

Deasyncify

A declarative way of doing asynchronous programing with Typescript.

Overview

Getting started

Deasyncify is a utility library which builds on top of Typescript's Promise api and simplifies handling promises and async functions. The goal of this library is to help make handling asychronous Typescript code more declarative withoud having to wrap code in a try-catch blockcode (which makes your code more cleaner) and without losing type definition on the return types of the values of resolved promises or async function trying to write a custom wrapper for yourself (yes we implemented that too). Imagine writing your own custom async wrapper for every single new project (that's pretty much a drag).

import axios from 'axios';
import Deasyncify from 'deasyncify';

const doSomething = async () => {
    const [expectedValue, error] = await Deasyncify.watch(axios.get(url))

    if(error) throw error

    return expectedValue
}

Issues

This library is meant to work on all nodejs enviroments. If any issue or bug is found in whatever enviroment you are on, feel free to report it to issues and we'll address it as fast as we can.

Installation

Using yarn

$ yarn add deasyncify

Using npm

$ npm install deasyncify

Usage

Deasyncify is a utility class which currenctly comprises of watch, watchAll and watchSettled methods which we'll look into below

Methods

watch

Deasyncify.watch is used to handle a single asynchorous function or promise.

Definitions

  • watch = <T> (asynParam: Promise<T> | (() => Promise<T>)) => Promise<[T | null, any]>
  • Args
    • asyncParam: Promise<T> | (() => Promise<T>)
  • Expected output:
    • expectValue: T | null
    • error: any

usage

watch takes in on argument (which could be either a promise or an async function or a function that returns a promise) executes it and returns an array of two values; expectedValue and error. expectedValue being the value return from your asynchronous code (you can name this anything) and error being any errors which running your asynchronous code (you can name this anything as well). error would be undefined if the asyncParam runs successfully but if asyncParam fails, error would be defined an expectedValue would be undefined.

Example

import { Request, Response, Next } from "express";
import prisma from "prisma";
import Deasyncify from "deasyncify";

export const getUser = async (req: Request, res: Response, next: Next) => {
    const [user, userFindError] = await Deasyncify.watch(this.prisma.users.findOne({
        where: { userId: req.user.id }
        }));

    if(userFindError) return next(userFindError);

    res.json(user);
};

watchAll

Deasyncify.watchAll is used to handle a bunch of asynchorous function or promise "concurrently".

Definitions

  •  watchAll = <
      J,
      T extends Awaited<J>,
      G = T extends () => any ? Awaited<ReturnType<T>> : T> (asyncArr: J[]) => Promise<[G[] | null, any]>
    
  • Args

    • asyncParam: (Promise<T> | (() => Promise<T>))[]
  • Expected output:

    • expectValues: T[] | null
    • error: any

usage

watchAll takes in on argument (which could be an array of an asyncParam (a promise or an async function or a function that returns a promise)) executes it and returns an array of two values; expectedValue and error. expectedValues being the value return from your asynchronous code (you can name this anything) and error being any error that occur while running your asynchronous code (you can name this anything as well). if any of the asyncParam in the arr fails, expectedValues would be null and error would be defined. An alternative to this is watchSettled method which doesn't have this behaviour.

Example

import { Request, Response, Next } from "express";
import prisma from "prisma";
import Deasyncify from "deasyncify";

export const getUserItems = async (req: Request, res: Response, next: Next) => {
    const [userItems, userItemsFindError] = await Deasyncify.watchAll([
        this.prisma.cars.findOne({where: { ownersId: req.user.id } }),
        this.prisma.phones.findOne({ where: { ownersId: req.user.id }}),
        this.prisma.pets.findOne({ where: { ownersId: req.user.id }})
        ]);

    if(userFindError) return next(userItemsFindError);

    res.json(userItems);
};

watchSettled

Deasyncify.watchSettled is used to also handle a bunch of asynchorous function or promise "concurrently".

Definitions

  •  watchSettled = <
      J,
      T extends Awaited<J>,
      G = T extends () => any ? Awaited<ReturnType<T>> : T> (asyncArr: J[]) => Promise<[G[], any[]]>
    
  • Args

    • asyncParam: (Promise<T> | (() => Promise<T>))[]
  • Expected output:

    • expectValues: T[] | null
    • errors: any[]

usage

watchAll takes in on argument (which could be an array of an asyncParam (a promise or an async function or a function that returns a promise)) executes it and returns an array of two values; expectedValue and error. expectedValues being the value return from your asynchronous code (you can name this anything) and error being an arr of errors which running your asynchronous code (you can name this anything as well). unlike watchAll which fails if one of the asyncArr elems fail during execution, since watchSettled using Promise.allSettled under the hood, expectedValues would contain all the values of the settled asyncParams and errors would contain all errors if any.

Example

import { Request, Response, Next } from "express";
import prisma from "prisma";
import Deasyncify from "deasyncify";

export const getUserItems = async (req: Request, res: Response, next: Next) => {
    const [userItems, userItemsFindError] = await Deasyncify.watchSettled([
        this.prisma.cars.findOne({where: { ownersId: req.user.id } }),
        this.prisma.phones.findOne({ where: { ownersId: req.user.id }}),
        this.prisma.pets.findOne({ where: { ownersId: req.user.id }})
        ]);

    if(userItemsFindError.length > 0) return next(userItemsFindError);

    res.json(userItems);
};