Skip to content

emitAsync

emitAsyncemit的异步版本,调用方法与emit相同。

emitAsync内部是调用emit实现的,代码如下:

ts
class FastEvent {
async emitAsync(){
    const results = await Promise.allSettled(this.emit.apply(this, arguments as any))
    return results.map((result) => {
        if (result.status === 'fulfilled') {
            return result.value
        } else {
            return result.reason
        }
    })
}

可以看到,emitAsync内部是对emit的返回值进行了Promise.allSettled处理,然后返回一个结果数组。

以下是emitAsyncemit的简单对比:

  • 同步监听器返回值
ts
emitter.on('event', () => 1);
emitter.on('event', () => 2);
emitter.on('event', () => 3);

const syncResults = emitter.emit('event', payload);
const asyncResults = await emitter.emitAsync('event', payload);

// syncResults === [1, 2, 3]
// asyncResults === [1, 2, 3]
  • 异步监听器返回值
ts
emitter.on('event', async () => 1);
emitter.on('event', async () => 2);
emitter.on('event', async () => 3);

const syncResults = emitter.emit('event', payload);
const asyncResults = await emitter.emitAsync('event', payload);

// syncResults === [Promise, Promise, Promise]
// asyncResults === [1, 2, 3]