Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

safe-async-wrapper

0.0.2 • Public • Published

safe-async-wrapper

Wrapper for async functions to always catch errors as first parameter of callback.

Installation -

npm install safe-async-wrapper

Problem -

You can catch the errors using try/catch blocks in synchronous functions. i.e.

try {
    syncFunction(a, b, c);
} catch (e) {
    console.log("Dhappa: ", e);
}

But this will not work with asynchromnous functions. For e.g.

function asyncFunction(a, b, c, callback) {
    setTimeout(function () {
        throw new Error("BINGO ERROR");
        callback();
    }, 1000);
}

try {
    asyncFunction(a, b, c, function () {
        console.log('asyncFunction completed');
    });
} catch (e) {
    console.log("I can't catch the error here");
}

Solution -

We can safely wrap our async function to get the uncaught exception as first parameter of the callback. i.e.

var safeAsyncWrapper = require('safe-async-wrapper');
var asyncFunctionWrapper = safeAsyncWrapper.wrap(asyncFunction);

asyncFunction(a, b, c, function (err) {
    if (err) {
        console.log('Now I can catch the BINGO ERROR here', err);
    }
});

Assumption: safe-async-wrapper expects last argument of async function to be some callback.

install

npm i safe-async-wrapper

Downloadsweekly downloads

1

version

0.0.2

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability