Skip to content

Commit

Permalink
docs: update example descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed Apr 30, 2024
1 parent c200763 commit ff06785
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
[![tree-shakable](https://img.shields.io/badge/tree-shakable-success)](https://bundlejs.com/?q=@wopjs/async-seq)
[![side-effect-free](https://img.shields.io/badge/side--effect-free-success)](https://bundlejs.com/?q=@wopjs/async-seq)

Run async functions in sequence.
Run async functions one-by-one in a sequence.

Docs: <https://wopjs.github.io/async-seq/>

## Install

Expand All @@ -24,6 +26,8 @@ npm add @wopjs/async-seq
import { seq } from "@wopjs/async-seq";

const s = seq();

// add async functions to the sequence and wait for the sequence to finish
await s.add(
() => {
const ticket = setTimeout(spy, 100);
Expand All @@ -35,20 +39,32 @@ await s.add(
}
);

// or manually wait for the sequence to finish
await s.wait();

// cancel all tasks
s.dispose();
```

By default the sequence has unlimited size. You can limit the size of the sequence by passing the `window` option.
Together with the `dropHead` option you can control the behavior of the sequence when it reaches its limit.

Simulate debounce:

```ts
const task = () => console.log("task");
const debounce = (task: () => void, ms: number) => {
const s = seq({ window: 1, dropHead: true });
return () =>
s.add(() => {
const ticket = setTimeout(task, ms);
return () => clearTimeout(ticket);
});
};

const debounced = debounce(() => console.log("task"), 100);

const s = seq({ window: 1, dropHead: true });
for (let i = 0; i < 10; i++) {
await s.add(() => {
const ticket = setTimeout(task, 100);
return () => clearTimeout(ticket);
});
debounced();
}
```

Expand Down

0 comments on commit ff06785

Please sign in to comment.