Giter VIP home page Giter VIP logo

await-of's Introduction

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

await-of's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar odykyi avatar semantic-release-bot avatar vadzim avatar xobotyi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

await-of's Issues

`[err, foo]` instead of `[foo, err]`

Do you want to request a feature or report a bug?

Feature


JS callback convention is like this. Can you provide an option to change it to this. Otherwise create library.

Question about throw undefined/Promise.reject handling

Hello, I just run in https://npm.runkit.com/await-of

const of = require("await-of").default

const f = () => Promise.reject();

let [res, err] = await of(f());

if (err) console.log('Error in of');

try {
    await f();
} catch (e){
    console.log('Error in try-catch');
}

and result is:

"Error in try-catch"

Same behavior would be in throw Error instead of Promise.reject(), maybe

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot 📦🚀

awaiting of non-thenable throws

await 1 works well, while await of(1) throws an error.

> await 1
1
> await of(1)
TypeError: promise.then is not a function
    at of (/tmp/await-of-test/node_modules/await-of/dist/await-of.js:14:20)
    at repl:1:30

Of course, it's a bit strange to just write await 1, but it could be expected that await-of should work nearly as an in-place replacement for await and should not throw errors where await does not.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.