---
title: "on | JavaScript maps SDK"
description: "Documentation for MapTiler SDK"
---

> For AI agents: a documentation index is available at [/llms.txt](https://docs.maptiler.com/llms.txt). Markdown versions of all pages are available by appending .md to any URL.

# Function on

-   on(  
        emitter: [EventEmitter](https://docs.maptiler.com/sdk-js/api-reference/classes/Externals.EventEmitter/),  
        eventName: string | symbol,  
        options?: [OnOptions](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Externals.EventEmitter.OnOptions/),  
    ): AsyncIterator<any\[\]\>[](#on)
    
    ```typescript
    import { on, EventEmitter } from 'node:events';
    import process from 'node:process';
    
    const ee = new EventEmitter();
    
    // Emit later on
    process.nextTick(() => {
      ee.emit('foo', 'bar');
      ee.emit('foo', 42);
    });
    
    for await (const event of on(ee, 'foo')) {
      // The execution of this inner block is synchronous and it
      // processes one event at a time (even with await). Do not use
      // if concurrent execution is required.
      console.log(event); // prints ['bar'] [42]
    }
    // Unreachable here
    ```
    
    Returns an `AsyncIterator` that iterates `eventName` events. It will throw if the `EventEmitter` emits `'error'`. It removes all listeners when exiting the loop. The `value` returned by each iteration is an array composed of the emitted event arguments.
    
    An `AbortSignal` can be used to cancel waiting on events:
    
    ```typescript
    import { on, EventEmitter } from 'node:events';
    import process from 'node:process';
    
    const ac = new AbortController();
    
    (async () => {
      const ee = new EventEmitter();
    
      // Emit later on
      process.nextTick(() => {
        ee.emit('foo', 'bar');
        ee.emit('foo', 42);
      });
    
      for await (const event of on(ee, 'foo', { signal: ac.signal })) {
        // The execution of this inner block is synchronous and it
        // processes one event at a time (even with await). Do not use
        // if concurrent execution is required.
        console.log(event); // prints ['bar'] [42]
      }
      // Unreachable here
    })();
    
    process.nextTick(() => ac.abort());
    ```
    
    #### Parameters
    
    -   emitter: [EventEmitter](https://docs.maptiler.com/sdk-js/api-reference/classes/Externals.EventEmitter/)
    -   eventName: string | symbol
    -   `Optional`options: [OnOptions](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Externals.EventEmitter.OnOptions/)
    
    #### Returns AsyncIterator<any\[\]\>
    
    `AsyncIterator` that iterates `eventName` events emitted by the `emitter`
    
    #### Since[](#since)
    
    v13.6.0, v12.16.0
    
-   on(  
        emitter: EventTarget,  
        eventName: string,  
        options?: [OnOptions](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Externals.EventEmitter.OnOptions/),  
    ): AsyncIterator<any\[\]\>[](#on-1)
    
    ```typescript
    import { on, EventEmitter } from 'node:events';
    import process from 'node:process';
    
    const ee = new EventEmitter();
    
    // Emit later on
    process.nextTick(() => {
      ee.emit('foo', 'bar');
      ee.emit('foo', 42);
    });
    
    for await (const event of on(ee, 'foo')) {
      // The execution of this inner block is synchronous and it
      // processes one event at a time (even with await). Do not use
      // if concurrent execution is required.
      console.log(event); // prints ['bar'] [42]
    }
    // Unreachable here
    ```
    
    Returns an `AsyncIterator` that iterates `eventName` events. It will throw if the `EventEmitter` emits `'error'`. It removes all listeners when exiting the loop. The `value` returned by each iteration is an array composed of the emitted event arguments.
    
    An `AbortSignal` can be used to cancel waiting on events:
    
    ```typescript
    import { on, EventEmitter } from 'node:events';
    import process from 'node:process';
    
    const ac = new AbortController();
    
    (async () => {
      const ee = new EventEmitter();
    
      // Emit later on
      process.nextTick(() => {
        ee.emit('foo', 'bar');
        ee.emit('foo', 42);
      });
    
      for await (const event of on(ee, 'foo', { signal: ac.signal })) {
        // The execution of this inner block is synchronous and it
        // processes one event at a time (even with await). Do not use
        // if concurrent execution is required.
        console.log(event); // prints ['bar'] [42]
      }
      // Unreachable here
    })();
    
    process.nextTick(() => ac.abort());
    ```
    
    #### Parameters
    
    -   emitter: EventTarget
    -   eventName: string
    -   `Optional`options: [OnOptions](https://docs.maptiler.com/sdk-js/api-reference/interfaces/Externals.EventEmitter.OnOptions/)
    
    #### Returns AsyncIterator<any\[\]\>
    
    `AsyncIterator` that iterates `eventName` events emitted by the `emitter`
    
    #### Since[](#since-1)
    
    v13.6.0, v12.16.0
