Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Handle Race Condition in Node Js using Mutex

import { Mutex, MutexInterface } from 'async-mutex';

class PaymentService {
	private locks : Map<string, MutexInterface>;

	constructor() {
		this.locks = new Map();
	}

	public async participateInFreeEvent(user: User, eventId: number): Promise<void> {
        if (!this.locks.has(user.id)) {
          this.locks.set(user.id, new Mutex());
        }
        
        this.locks
            .get(user.id)
            .acquire()
            .then(async (release) => {
                try {
                    const existOrder = await findOrder(eventId, user.id);
                    if (!existOrder) {
                        const order = buildNewOrder(eventId, user.id);
                        createOrder(order.id, eventId, user.id);
                    }
                } catch (error) {
                } finally {
                    release();
                }
            },
        );
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to jump one page to another on specific tab elementor 
Javascript :: Use ChainLink Feed Registry 
Javascript :: jstree get not disabled nodes 
Javascript :: detect paste in textarea 
Javascript :: exit from jshell 
Javascript :: How to make a json call to a URL 
Javascript :: Perform native operation by javascript in Android 
Javascript :: nuxt js index.html not found 
Javascript :: top 50 mcq que in javascript 
Javascript :: what is fn extend 
Javascript :: vanilla javascript event when reach bottom of element no jquery 
Javascript :: bootstrap dropdown with state 
Javascript :: getComments 
Javascript :: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace 
Javascript :: react native paper status bar color 
Javascript :: Alternative Syntax For Backbone Simple Template 
Javascript :: remove package-lock.json from commit 
Javascript :: Remove the minimum 
Javascript :: a critical point in an array is defined as either a local maxima or a local minima 
Javascript :: GridFs Schema 
Javascript :: jquery for get object in 2nd or 3rd place 
Javascript :: invalid json text mysql 
Javascript :: How to Solve the Staircase Problem with 5 Lines of JavaScript 
Javascript :: Colored tab in react Js MUI 
Javascript :: mvc return view with query string 
Javascript :: what is container in angular 
Javascript :: get aggregate sum value in kendo grid footer jquery 
Javascript :: select item from list javascript 
Javascript :: Could not resolve dependency: npm ERR! peer react@"^16.0.0" from react-acceptjs@0.1.2 
Javascript :: how to fetch devto api 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =