ao

await-of

await wrapper for easier errors handling without try-catch

Showing:

Popularity

Downloads/wk

1.3K

GitHub Stars

245

Maintenance

Last Commit

2d ago

Contributors

5

Package

Dependencies

0

License

MIT

Type Definitions

Built-In

Tree-Shakeable

Yes?

Categories

Reviews

Average Rating

5.0/51
Read All Reviews
ajayesivan

Top Feedback

1Easy to Use

Readme

await-of

await wrapper for easier errors handling without try-catch

NPM Version NPM Downloads NPM Dependents Build Coverage Types Tree Shaking


❤️Please consider starring this project to show your love and support.🙌

About

ES7 async/await gives to developers ability to write asynchronous code that look like synchronous. But under the hood it is still just a sugar on top of the ES6 Promise.
You can write code that looks clean, but only unless you have to catch errors. To catch thrown error or handle the promise's rejection you have to surround it with try-catch block or fallback to pure promises and from that moment visual purity of your code is over.
But there is a solution!☀️
I really like the way it's done in Go. It has no error throwing mechanism, but has a multi-value return and the common way to handle errors in Go is to return error as a last value, like so:

data, err := someErrorFunc(someStuff)
if err != nil {
    return err
}

But JavaScript has no multi-value return! - you would say. Sad, but true.
But!
It has a destructuring assignment and await-of gives you ability to do this:

import { of } from "await-of";

async () => {
  let [res, err] = await of(axios.get("some.uri/to/get"));

  if (err) {
    // rethrow if its not an axios response error
    if (!err.response) {
      throw err;
    }

    res = err.response;
  }

  const { data, status = 0 } = res;

  console.log(data, status);
};

There is no modifications needed in function/promise you want to await - just pass it to the of() and whole the magic will be done.

Installation

npm i --save await-of

Usage

import { of } from "await-of";

async function someAsyncStuff() {
  let error, data;

  // if we don't want to handle error
  [data] = await of(Promise.reject(new Error("ERROR!")));
  console.log(data); // undefined

  // if promise was rejected - it's rejection value will be treated as error
  [, error] = await of(Promise.reject(new Error("ERROR!")));
  console.log(error); // ERROR!

  // or if promise has any uncaught errors it'll catch them too!
  [, error] = await of(
    new Promise(() => {
      throw new TypeError("ERROR!");
    })
  );
  console.log(error.message); // ERROR!
}

Tests

# install dependencies if you haven't yet
npm install
npm run test

Rate & Review

Great Documentation0
Easy to Use1
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
Ajay SivanIndia32 Ratings44 Reviews
Engineering Manager @bigbinary
3 months ago
Easy to Use

Handling asynchronous code is always been a pain. Even though we have come a long way from callbacks to promises to async/await. Still, it doesn't feel like writing synchronous code. I think this library is trying to improve that situation further by adding syntactic sugar on top of async/await to provide a more synchronous like feel. The best part is that we don't have to write any more try-catch.


2
preveenraj
vishnuprasad-95