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 🙏

© 2026 – Pkg Stats / Ryan Hefner

async-expect

v1.0.0

Published

Efficient error handling in async-await.

Readme

async-expect

Expectation function taken from expect. This is not for writing test but to use it when writing asynchronus code with the help of async-await. (see below for example use-case)

Usage

Installation

Using npm:

$ npm install --save expect

Then, use as you would anything else:


const expect = require("async-expect")

Use Case

When using async-await it becomes hard to catch errors. We have to wrap each await call to try-catch block to specifically handle that error. Wrapping multiple await calls in one try-catch block will create problem which await call thrown this error.
Example:

Without async-expect

1. One try-catch block

try{ 
    let user = await User.find(ctx.body.username);

    let like = await Post.addLike(user.username);

    let somthingelse = await forSomethingelse();

}catch(err){
    // no way to get find which call returned that error.
    // if those are third party lib function then you'll have rely
    // on regex to find error type and etc
    console.log(err) // nothing you can do at this point.
}

2. Multiple try-catch block

try{
    // let user = await User.find(ctx.body.username);
    var user = await User.find(ctx.body.username);
}catch(err){
    app.redirect("/err", "invalid user")
}

try{
    // problem1 - user isnt accessible here. let block scoped
    // have to user var
    // let like = await Post.addLike(user.username);
    var like = await Post.addLike(user.username);
}catch(err){
    app.redirect("/err", "invalid user")
}

// problem2 - readablity issue

3. Using async-expect


const expect = require("async-expect");

try{ 
    let user = await User.find(ctx.body.username);
    expect(user.username).toExists("Username is invalid", "INVALID-USER")

    let like = await Post.addLike(user.username);
    expect(like.status).toBe(true, 
        "User doesn't have permission to see", "PERM-DENIED")

    let somthingelse = await forSomethingelse();
    expect(somethingelse).toBe(Object, "something else is wrong", "SOMETHING-ERROR", somethingelse)

}catch(err){
    switch(err.name){
        case: "INVALID-USER":
            app.redirect("/register", "Please register to continue");
            break;
        case: "PERM-DENIED":
            app.redirect("/user", "You dont have enough permission");
            break;
        case: "SOMETHING-ERROR":
            let somethingelse = err.extra
            doSomething(somethingelse);
            break;
    }
}

Assertions

Currently it contains 7 assertive function which is more than enough to be useful in conjunction with async-await.

Note: [error-message, error-name, error-extra] parameters are optional.

toExist

expect(object).toExist([error-message, error-name, error-extra])

Asserts the given object is truthy.

expect('something truthy').toExist()

toNotExist

expect(object).toNotExist([error-message, error-name, error-extra])

Asserts the given object is falsy.

expect(null).toNotExist()

toBe

expect(object).toBe(value, [error-message, error-name, error-extra])

Asserts that object is equal to value using ==.

try { 
  let role = await User.getRole();
  expect(role).toBe("admin", "Only admin user is allowed", "NotAdminError");
}catch(err){
  if(err.name == "NotAdminError"){
    app.redirect("/user", {message: "You're not allowed to perform this operation"});
  }
}

toNotBe

expect(object).toNotBe(value, [error-message, error-name, error-extra])

Asserts that object is not equal to value using !=.

toBeA(constructor)

expect(object).toBeA(constructor, [error-message, error-name, error-extra]) expect(object).toBeAn(constructor, [error-message, error-name, error-extra])

Asserts the given object is an instanceof constructor.

expect(new User).toBeA(User)
expect(new Asset).toBeAn(Asset)

toBeA(string)

expect(object).toBeA(string, [error-message, error-name, error-extra]) expect(object).toBeAn(string, [error-message, error-name, error-extra])

Asserts the typeof the given object is string.

expect(2).toBeA('number')

toNotBeA(constructor)

expect(object).toNotBeA(constructor, [error-message, error-name, error-extra]) expect(object).toNotBeAn(constructor, [error-message, error-name, error-extra])

Asserts the given object is not an instanceof constructor.

expect(new Asset).toNotBeA(User)
expect(new User).toNotBeAn(Asset)

toNotBeA(string)

expect(object).toNotBeA(string, [error-message, error-name, error-extra]) expect(object).toNotBeAn(string, [error-message, error-name, error-extra])

Asserts the typeof the given object is not string.

expect('a string').toNotBeA('number')
expect(2).toNotBeAn('object')