Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

async mutex

import { Semaphore } from 'async-mutex'
import fs from 'fs/promises'

async function asyncProcessWithMutex() {
	let semaphore = new Semaphore(3) // set to 3 because this to handle 3 async process, if you only handle 1 async use Mutex
	let [isNumber, isRelease] = await semaphore.acquire()
	try {
		const data1 = await fs.readFile('index1.txt')
		console.log(data1.toString())

		const data2 = await fs.readFile('index2.txt')
		console.log(data2.toString())

		const data3 = await fs.readFile('index3.txt')
		console.log(data3.toString())
	} catch (e) {
		console.error(e)
		isRelease()
	} finally {
		isRelease()
	}
}

async function asyncProcessWithoutMutex() {
	try {
		const data1 = await fs.readFile('index1.txt')
		console.log(data1.toString())

		const data2 = await fs.readFile('index2.txt')
		console.log(data2.toString())

		const data3 = await fs.readFile('index3.txt')
		console.log(data3.toString())
	} catch (e) {
		console.error(e)
	}
}

setInterval(async () => {
	await asyncProcessWithMutex()
	console.log('process with mutex 
')
}, 2000)

setInterval(async () => {
	await asyncProcessWithoutMutex()
	console.log('process without mutex 
')
}, 2500)
Comment

PREVIOUS NEXT
Code Example
Javascript :: ngrx let 
Javascript :: coin gecko api 
Javascript :: how to add link during filter and mapping in javascript 
Javascript :: get lat long from address google api 
Javascript :: javascript get value outside function 
Javascript :: how to build a nested, dynamic JSON in Go 
Javascript :: how to hide prerendered page button in nextjs 
Javascript :: error first callback in node js 
Javascript :: to fix a broken class oop javascript 
Javascript :: react native mirror text 
Javascript :: react native push notifications cancel delivered notification 
Javascript :: Appium find Android Element with Xpath using Javascript 
Javascript :: jquery search button 
Javascript :: Creating Multiple Methods From A List Of Words 
Javascript :: controllare che ci sia un file in javascript 
Javascript :: react native text input allow only numbers 
Javascript :: Object methods + Static methods javascript 
Javascript :: SordMap elo 
Javascript :: react_devtools_backend.js:4026 Warning: Cannot update a component (`BrowserRouter`) while rendering a different component (`Login`). 
Javascript :: cubing timer 
Javascript :: Inside Fetch Is A Request 
Javascript :: prisma bytes 
Javascript :: NavBar with divs 
Javascript :: Backbone View Event 
Javascript :: Backbone + Express 
Javascript :: React Futures - Server Components 
Javascript :: java scrypt 
Javascript :: how to check the validation of time in react datetime 
Javascript :: var vs let javascript 
Javascript :: 2d arrays js 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =