Horje
Handle Race Condition in Node Js using Mutex Code Example
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();
                }
            },
        );
    }
}




Javascript

Related
javascript custom modal Code Example javascript custom modal Code Example
10.8.1.2. The isPalindrome Function Code Example 10.8.1.2. The isPalindrome Function Code Example
enable clipboard Code Example enable clipboard Code Example
tab change hash Code Example tab change hash Code Example
jquery daterangepicker using moment Code Example jquery daterangepicker using moment Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8