Skip to content

Commit 9c3c42f

Browse files
Add(examples): Create an example for blPop & lPush (#1696)
* Add(examples): Create an example for blPop & lPush Signed-off-by: Aditya Rastogi <[email protected]> * Update(examples): fix case, add timeout, update readme Signed-off-by: Aditya Rastogi <[email protected]> Closes #1693.
1 parent 9a4ac34 commit 9c3c42f

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

examples/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
This folder contains example scripts showing how to use Node Redis in different scenarios.
44

5-
| File Name | Description |
6-
| ------------------------ | ----------------------------------------------------------------------------- |
7-
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
5+
| File Name | Description |
6+
|--------------------------|--------------------------------------|
7+
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
8+
| `blocking-list-pop.js` | Block until an element is pushed to a list |
89
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
910

1011
## Contributing

examples/blocking-list-pop.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This example shows how to use the blocking LPUSH command.
2+
3+
// This code shows how to run with isolation the blPop Command to block the script while waiting for a value to be pushed to the list.
4+
// The script will be blocked until the LPUSH command is executed.
5+
// After which we log the list and quit the client.
6+
7+
import { createClient, commandOptions } from 'redis';
8+
9+
async function blockingListPop() {
10+
const client = createClient();
11+
12+
await client.connect();
13+
14+
const keyName = 'keyName';
15+
16+
const blpopPromise = client.blPop(
17+
commandOptions({ isolated: true }),
18+
keyName,
19+
0
20+
);
21+
22+
await client.lPush(keyName, 'value');
23+
24+
await blpopPromise;
25+
26+
console.log('blpopPromise resolved');
27+
console.log(keyName);
28+
29+
await client.quit();
30+
}
31+
32+
blockingListPop();

0 commit comments

Comments
 (0)