mutexify

let fs = require('fs/promises')
let mutexify = require('mutexify/promise')

async function asyncProcessWithMutexify() {
	try {
		let lock = await mutexify()

		let isRelease = await lock()
		fs.readFile('index1.txt').then((data) => {
			console.log('asyncProcessWithMutexify - ' + data.toString())
			isRelease()
		})

		isRelease = await lock()
		fs.readFile('index2.txt').then((data) => {
			console.log('asyncProcessWithMutexify - ' + data.toString())
			isRelease()
		})

		isRelease = await lock()
		fs.readFile('index3.txt').then((data) => {
			console.log('asyncProcessWithMutexify - ' + data.toString())
			isRelease()
		})
	} catch (e) {
		console.error(e)
	}
}

async function asyncProcessWithoutMutexify() {
	try {
		fs.readFile('index1.txt').then((data) => {
			console.log('asyncProcessWithoutMutexify - ' + data.toString())
		})

		fs.readFile('index2.txt').then((data) => {
			console.log('asyncProcessWithoutMutexify - ' + data.toString())
		})

		fs.readFile('index3.txt').then((data) => {
			console.log('asyncProcessWithoutMutexify - ' + data.toString())
		})
	} catch (e) {
		console.error(e)
	}
}

setInterval(async () => {
	await asyncProcessWithMutexify()
	await asyncProcessWithoutMutexify()
}, 2000)
Restu Wahyu Saputra