最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Async Event Listener with Nestjs EventEmitter Module and Serverless functions - Stack Overflow

programmeradmin2浏览0评论

I'm trying to implement an asynchronous worker with serverless lambda function with help of nestjs EventEmitter module.

Handler is getting invoked on emitting an event, but the function gets closed before the async / await call.

I have tried using both emit and emitAsync functions and different arguments for @OnEvent(AccountEvents.NEW_ACCOUNT, {async:true, promisify:true})

Producer Snippet

  public async execute(event: EventDetail): Promise<void> {

    await this.eventEmitter.emitAsync(AccountEvents.NEW_ACCOUNT, event);
  }

Listener Snippet

@OnEvent(AccountEvents.NEW_ACCOUNT)
  public async handleAccountCreatedEvent(event: EventDetail): Promise<void> {
    this.logger.log({ message: `Log1: ${AccountEvents.NEW_ACCOUNT} Handler`, event });
    const message = await this.testAsync();
    this.logger.log({ message });
    this.logger.log({ message: 'Log 3: Event Processing Successfuly Completed' });
  }

  private testAsync(): Promise<string> {
    return new Promise(res => {
      setTimeout(() => {
        res('Log 2: Promise resolved after one sec');
      }, 1000);
    });
  }

Expect Output : all the 3 log statements Actual Output : only first log statement (Log1)

I'm trying to implement an asynchronous worker with serverless lambda function with help of nestjs EventEmitter module.

Handler is getting invoked on emitting an event, but the function gets closed before the async / await call.

I have tried using both emit and emitAsync functions and different arguments for @OnEvent(AccountEvents.NEW_ACCOUNT, {async:true, promisify:true})

Producer Snippet

  public async execute(event: EventDetail): Promise<void> {

    await this.eventEmitter.emitAsync(AccountEvents.NEW_ACCOUNT, event);
  }

Listener Snippet

@OnEvent(AccountEvents.NEW_ACCOUNT)
  public async handleAccountCreatedEvent(event: EventDetail): Promise<void> {
    this.logger.log({ message: `Log1: ${AccountEvents.NEW_ACCOUNT} Handler`, event });
    const message = await this.testAsync();
    this.logger.log({ message });
    this.logger.log({ message: 'Log 3: Event Processing Successfuly Completed' });
  }

  private testAsync(): Promise<string> {
    return new Promise(res => {
      setTimeout(() => {
        res('Log 2: Promise resolved after one sec');
      }, 1000);
    });
  }

Expect Output : all the 3 log statements Actual Output : only first log statement (Log1)

Share Improve this question asked Dec 26, 2021 at 13:21 SedhuSedhu 7731 gold badge13 silver badges36 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Making Changes as follows works. Basically we need pass { async: true, promisify: true } to the annotation @onEvent.

 @OnEvent(AccountEvents.NEW_ACCOUNT, { async: true, promisify: true })
  async handleNewAccountEvent(event: IEvent<RootAccountInterface>): Promise<void> {
    const eventDetail: RootAccountInterface = event.data;
    this.logger.log({ message: ` ${AccountEvents.NEW_ACCOUNT} Handler`, event });
    await this.abc.logNewAccountCreation(eventDetail.accountId);

    this.logger.log({ message: ` ${AccountEvents.NEW_ACCOUNT} Handled Successfully`, event });
  }

EventEmmiter2 docs

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论