Skip to content

Commit 4703cee

Browse files
Boris Dorofeevnodkz
authored andcommitted
refactor: return promis from deleteQueue
1 parent 9e583a6 commit 4703cee

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/helpers/deleteKeys.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MutationError, ErrorCodeEnum } from './MutationError';
22
import { getBullConnection } from './getBullConnection';
33
import { Options } from '../definitions';
4+
import { resolve } from 'dns';
45

56
export async function deleteQueue(
67
prefix: string,
@@ -20,18 +21,26 @@ export async function deleteQueue(
2021
}
2122
}
2223

23-
//redis-cli: scan 0 match fullName* count 300
24-
const stream = connection.scanStream({ match: fullName + '*', count: 300 });
24+
const total = await new Promise<number>((resolve, reject) => {
25+
//redis-cli: scan 0 match fullName* count 300
26+
const stream = connection.scanStream({ match: fullName + '*', count: 300 });
2527

26-
let total = 0;
28+
let total = 0;
2729

28-
stream.on('data', async (keys) => {
29-
for (let i = 0; i < keys.length; i++) {
30-
const del = await connection.del(keys[i]);
31-
if (del) {
32-
total++;
30+
stream.on('data', async (keys) => {
31+
for (let i = 0; i < keys.length; i++) {
32+
const del = await connection.del(keys[i]);
33+
if (del) {
34+
total++;
35+
}
3336
}
34-
}
37+
});
38+
39+
stream.on('end', () => {
40+
resolve(total);
41+
});
42+
43+
stream.on('error', reject);
3544
});
3645

3746
return total;

0 commit comments

Comments
 (0)