Skip to content

Convert any callback-based sequence of values into a full-fledged async iterable

License

Notifications You must be signed in to change notification settings

shtaif/iterified

Folders and files

NameName
Last commit message
Last commit date

Latest commit

a26909d · Aug 18, 2023

History

12 Commits
Aug 18, 2023
Aug 17, 2023
Aug 17, 2023
Aug 17, 2023
Aug 17, 2023
Aug 17, 2023
Aug 17, 2023
Aug 17, 2023
Aug 17, 2023
Aug 18, 2023
Aug 17, 2023
Aug 16, 2023
Aug 18, 2023
Aug 17, 2023
Aug 17, 2023
Aug 17, 2023

Repository files navigation

Iterified

...

import { asyncIterify } from 'asyncIterify';
import redis from 'redis';

const channelsToSubscribe = ['my-channel-1', 'my-channel-2'];

const redisChannelMessages = asyncIterify(async (next, done, error) => {
  const messageHandler = (channel, message) => next({ channel, message });

  const currentSubscribedChannelsCount = await redis.subscribe(...channelsToSubscribe);
  redis.on('message', messageHandler);

  return async () => {
    await redis.unsubscribe(...channelsToSubscribe);
    messageHandler.off(messageHandler);
  };
});

(async () => {
  for await (const { channel, message } of redisChannelMessages) {
    console.log(`Received ${message} from ${channel}`);
  }
})();