Giter VIP home page Giter VIP logo

await-of's Introduction

await-of

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 cleanliness 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("ERROR!"));
    console.log(data); // undefined
    
    // if promise was rejected - it's rejection value will be treated as error
    [, error] = await of(Promise.reject("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

Coverage

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

await-of's People

Contributors

odykyi avatar vadzim avatar xobotyi avatar

Watchers

 avatar  avatar

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.