`npm i @joesonw/tslib`
npm install @joesonw/tslibnpm i @joesonw/tslib
ts
interface Worker {
work(task: T): Promise;
cancel?(): void;
}
`$3
helper function to build simple worker`
new ThrottledWorkerPool(ThrottledFunction(task => console.log(task)));
`$3
create a ThrottledWorkerPool$3
enqueue a task$3
start the pool with given concurrency$3
stop the pool and wait all worker done/canceleddefer
$3
`ts
const f = defer.sync((defer) => (name: string): string => {
defer((result, err) => {
if (err) {
return 'something went wrong';
}
return 'nihao, ' + name;
}) // last
defer((result, err) => {
if (err) {
return 'something went wrong';
}
return 'hola, ' + name;
}) // first
if (Date.now() % 2 == 1) throw new Error('oops');
return 'hello,' + name;
});f('xiaoming');
`$3
`ts
const f = defer.async((defer) => async (name: string): string => {
defer(async (result, err) => {
if (err) {
return 'something went wrong';
}
return 'nihao, ' + name;
})
if (Date.now() % 2 == 1) throw new Error('oops');
return 'hello,' + name;
});
await f('xiaoming');
`Condition\
$3
wait for signal/broadcast$3
signal one waiting$3
wakeup/signal allMutex
lock
$3
$3
RWMutex
read/write lock
$3
can only obtain the lock if no other lock/rlock called$3
$3
can simultaneously obtain multiple read lock $3
sleep(ms: number): Promise
Once
$3
only one function will be executed at once, all other do` calls will be discarded before the first one is returned.