Trying to convert the following code (ioredis):
// Get JSON array of all currently active game IDs.
app.get('/api/activegames', async (req: Request, res: Response) => {
console.log(req); //gets rid of typescript error.
// Scan through all keys in the stream starting with "kaboom:moves".
const stream = redis.scanStream({
match: 'kaboom:moves:*'
});
const gameIds:any = [];
stream.on('data', (keys) => {
// Extract the gameId from the key and append to gameIds array.
keys.forEach((key) => gameIds.push(key.split(':')[2]));
});
stream.on('end', () => {
res.status(200).json({
data: {
gameIds,
length: gameIds.length
},
status: 'success',
})
})
});
This is what I have so far, not working:
app.get('/api/activegames', async (req: Request, res: Response) => {
const stream = client.scanIterator({
TYPE: 'string', // `SCAN` only
MATCH: 'kaboom:moves:*',
COUNT: 100
});
const LIMIT = 3;
const asyncIterable = {
[Symbol.asyncIterator]() {
let i = 0;
return {
next() {
const done = i === LIMIT;
const value = done ? undefined : i++;
return Promise.resolve({ value, done });
},
return() {
// This will be reached if the consumer called 'break' or 'return' early in the loop.
return { done: true };
}
};
}
};
(async () => {
for await (const keys of stream) {
gameIds.push(keys.split(':')[2]);
}
})();
const gameIds:any = [];
(async () => {
for await (const keys of stream) {
gameIds.push(keys.split(':')[2]);
}
})().then(
res.status(200).json({
data: {
gameIds,
length: gameIds.length
},
status: 'success',
}));
});
This is from: