sequencialize
Run async functions in sequence. Queue async functions.
How to make parallel-running async function run sequencially?
This module tries to resolve the problem when something like findAndUpdate
creates two entries when run almost parallel.
So, we just chain them.
If you are using message broker to queue potentially-breaking parallel function calls, you won't need this.
Usage
const Sequencer = const sequencer = await sequencer
It will return the value returned from the original function. So:
const n = 3const func = async n forlet i = n;i;i-- sequencer // 3// 2// 1
It will run functions in sequence:
const wait = const n = 3const func = { return } forlet i = n;i;i-- sequencer // 3// 2// 1
you can use namespace. Each namespace is it's own sequence:
const wait = const n = 3const func = { return } forlet i = n;i;i-- sequencer sequencer // each function in a namespace runs in sequence// asdf: 3// ASDF: 3// asdf: 2// asdf: 1// ASDF: 2// ASDF: 1
Use Case
instead of this:
// potentially racingconst handleRequest = { db }
write this:
// run in sequence for every user_idconst handleRequest = { sequencer user_id) }
Cheers, juji