1.2.22 • Published 2 years ago

asyncback v1.2.22

Weekly downloads
55
License
MIT
Repository
github
Last release
2 years ago

asyncback

npm downloads Open issues Pull requests

Convert async functions to callback style functions.

Why ?

With the introduction of async/await, writing async code has become much simpler. However, there still exists Javascript API that requires you to pass in callback accepting functions. For example, ExpressJS requires middleware functions to be plain old functions that accept a next callback.

The asyncback module converts an async function to a callback accepting function.

In Short

This:

async function(arg1, arg2, .., argN)

is converted to:

function(arg1, arg2, .., argN, callback)

Which is awesome in cases like writing an expressjs request handler, while using mongoose models:

const asyncback = require('asyncback');

app.get('/users', asyncback(async (req, res) => {
    const users = await User.find({ 'banned': false });
    const offer = await Offers.findOne({ 'active': true });
    res.json({ 'users': users, 'offer': offer });
}));

Usage

Install module:

npm install asyncback --save

Wrap async functions with asyncback before registering them with ExpressJS:

const asyncback = require('asyncback');

app.get('/path', asyncback(async (req, res) => {
    // async await style code
}));

Better yet, if middleware functions are in separate files, simply export the wrapped function:

const asyncback = require('asyncback');

async function someMiddleware(req, res){
    // async await style code
}

module.exports = asyncback(someMiddleware);

Also, if the middleware decides to stop next callback from being called:

const asyncback = require('asyncback');

app.get('/file', asyncback(async (req, res) => {
    // async await style code
    // start streaming, proceed no further
    return asyncback.NO_CB;
}));

Notes

  1. The callback will be automatically called after async function returns.
  2. Any error thrown by the async function is passed as the first parameter to callback.
  3. The return value of async function is passed as the second parameter to callback.
  4. In cases where the callback should not be automatically called, the async function can return a special value: asyncback.NO_CB.

Flexibility

The asyncback function is really generic that it converts an async function into a callback style function which accepts a callback as its last parameter. Hence its usage is not limited to just ExpressJS middleware. This also works for functions that return a promise or promise-like result.

Safety

The asyncback can safely wrap non async/promised functions and the returned function can be invoked without the callback parameter. In such cases the wrapped function will be called synchronously and with all supplied arguments. However such usage is discouraged as it serves no beneficial purpose.

Licence

The source code is available under MIT Licence.

1.2.22

2 years ago

1.2.21

5 years ago

1.2.5

6 years ago

1.2.2

6 years ago

1.1.2

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago