@vadzim/synchronized

1.0.1 • Public • Published

synchronized

Java synchronized keyword, but for javascript.

If an asynchronous function is decorated with synchronized, it suspends calls to this function until all the previous calls are finished. This is true for any function that returns a promise.

Usage:

import {synchronized} from '@vadzim/synchronized'

const asyncFunction = synchronized(async (a, b, c) => {
	...	
})

const promiseFunction = synchronized((a, b, c) => {
	...	
	return new Promise(...)
})

If used as a decorator, it makes method to be synchronized only within instance of a class. That means that executing method once at a time is restricted only for one instance, other instances can have their own running methods at that moment.

import {synchronized} from '@vadzim/synchronized'

class MyClass {
	@synchronized
	async asyncMethod(a, b, c) {
		....
	}

	@synchronized
	promiseMethod(a, b, c) {
		...
		return new Promise(...)
	}
}

If you want to get the decorator behavior but with a function call, you can use synchronizedMethod function.

import {synchronizedMethod} from '@vadzim/synchronized'

function MyClass(...) {...}

MyClass.prototype.method = synchronizedMethod(function(a, b, c) {...})

Readme

Keywords

none

Package Sidebar

Install

npm i @vadzim/synchronized

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • vadzim