Skip to content

Commit

Permalink
doc: update usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Jannchie committed Mar 19, 2021
1 parent a24885f commit f019667
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
## Usage

``` ts
import { parseCookies, stringifyCookies } from "jsemaphore";
cookies = "test=value; name=jsemaphore";
let cookiesMap = parseCookies(cookies);
cookiesMap.set("test", "new_value");
const cookiesString = stringifyCookies(cookiesMap);
// Simulate tasks that take 1 second
async function task(s: Semaphore) {
await new Promise((resolve) => setTimeout(() => resolve(null), 1000));
s.release();
}
// Concurrency = 10
const s = new Semaphore(10);

// jest
expect(cookiesString).toBe("test=new_value; name=jsemaphore");
const tasks = new Array<Promise<void>>();

// For total 40 tasks
for (let i = 0; i < 40; i++) {
await s.acquire();
tasks.push(task(s));
}

await Promise.all(tasks); // The total cost time is about 4 seconds
```

0 comments on commit f019667

Please sign in to comment.