async-mutex-wrap

1.0.3 • Public • Published

async-mutex-wrap

A singleton-backed wrapper that can prevent parallel calls to a function.

Example

Use case: the database is slow, and the goal is to optimize simultaneous requests for the same data

This code ensures that if a second client requests the same user data as another client still waiting for the response, both clients receive the same response object, and the database is only queried once.

const mutexify = require('async-mutex-wrap')
 
// apply mutex to your async function (any function that returns a promise)
const mutexedLookupUser = mutexify(lookupUserAsync)
 
const app = async function (req, res, next) {
  const user = await mutexedLookupUser(req.params.id)
  res.json(user)
}

The mutex uses both the function and the params passed as mutex keys. See mutexify.spec.js for detailed examples.

/async-mutex-wrap/

    Package Sidebar

    Install

    npm i async-mutex-wrap

    Weekly Downloads

    4

    Version

    1.0.3

    License

    ISC

    Unpacked Size

    4.16 kB

    Total Files

    6

    Last publish

    Collaborators

    • mz3