Overview
Removes and returns the first element of the list stored at key. When a count is provided, removes and returns up to count elements.
Method Signature
lpop<TData = string>(key: string): Promise<TData | null>
lpop<TData = string[]>(key: string, count: number): Promise<TData | null>
Parameters
The number of elements to pop. When specified, returns an array of elements instead of a single element.
Return Value
When called without count: The first element of the list, or null if the list is empty or does not exist.When called with count: An array of popped elements, or null if the list is empty or does not exist.
Examples
Basic Usage
import { Redis } from '@upstash/redis';
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
// Set up a list
await redis.lpush('mylist', 'world', 'hello');
// List is: ['hello', 'world']
// Pop the first element
const element = await redis.lpop('mylist');
console.log(element); // 'hello'
// Pop from empty list
const empty = await redis.lpop('nonexistent');
console.log(empty); // null
Pop Multiple Elements
// Set up a list
await redis.rpush('queue', 'job-1', 'job-2', 'job-3', 'job-4');
// Pop multiple elements
const jobs = await redis.lpop<string[]>('queue', 2);
console.log(jobs); // ['job-1', 'job-2']
Working with Objects
interface Task {
id: string;
title: string;
priority: number;
}
// Add tasks
await redis.lpush<Task>('tasks', {
id: '123',
title: 'Complete documentation',
priority: 1,
});
// Pop the task
const task = await redis.lpop<Task>('tasks');
console.log(task?.title); // 'Complete documentation'
Queue Processing
// Process items from the head of a queue
while (true) {
const item = await redis.lpop('queue:processing');
if (!item) {
break; // Queue is empty
}
// Process the item
await processItem(item);
}
Batch Processing
// Process multiple items at once
const batchSize = 10;
const batch = await redis.lpop<string[]>('queue:batch', batchSize);
if (batch) {
await Promise.all(batch.map(item => processItem(item)));
}
See Also
- RPOP - Remove and return the last element of a list
- LPUSH - Insert elements at the head of a list
- LRANGE - Get a range of elements from a list
- LLEN - Get the length of a list