async/await for optimistic concurrency control. (Last in wins.)
npm install jackall
Please refer to tests.
import { jackall } from "jackall";
// async tasks.
const task1 = async () => new Promise((ok) => setTimeout(ok, 200, "ping1"));
const task2 = async () => new Promise((ok) => setTimeout(ok, 100, "ping2"));
// jackall.acquire. (resource name is `tiger`)
const [act1, act2] = await Promise.all([
jackall.acquire("tiger", task1()),
jackall.acquire("tiger", task2()),
]);
// last in wins.
// act1 => { ok: false, code: JACKALL_ERROR_CODE.NOMATCH_VERSION, value: new Error(`Jackall Error: ${JACKALL_ERROR_CODE.NOMATCH_VERSION}`) }
// act2 => { ok: true, value: "ping2" }
If you want to specify a resource name.
import { makeJackall } from "jackall";
const jackall = makeJackall<"tiger">();
// jackall.acquire("tiger", task1()) => OK
// jackall.acquire("bear", task1()) => NG
import { JACKALL_ERROR_CODE } from "jackall";
option | description |
---|---|
FAILED_ASYNCTASK | Failed to Async task. |
NOMATCH_VERSION | Lost in the race conditions.(Last in Wins.) |
Jackall is MIT licensed.