AsyncMap class

Asynchronous version of standard Map class

This is an asynchronous analogue of the standard JavaScript Map class.

AsyncMap can serve as the foundation for custom Map-like classes that represent high-latency resources, such as server data or data which is slow to compute, which are accessed with asynchronous calls. For a synchronous version of this class, see SyncMap.

The methods and properties of AsyncMap all generally behave like their counterparts in Map, with the significant difference that they are all asynchronous.

Async iterators #

The standard Map class includes a number of methods that return an Iterator:

  • entries
  • keys
  • values
  • [Symbol.Iterator]

In AsyncMap, all of these methods return an AsyncIterator. Moreover, where Map uses [Symbol.Iterator], AsyncMap uses [Symbol.asyncIterator] instead.

Async iterators (marked with a * in the API documentation below) yield a sequence of values. To convert an async iterator to a synchronous form, such as an Array, it is generally necessary to use a JavaScript for await..of loop:

const m = new ExplorableSiteMap("https://weborigami.org");

// Collect all the keys from the top level of the site
const keys = [];
for await (const key of m.keys()) {
  keys.push(key);
}

For reference: JavaScript allows regular synchronous Iterator instances, such as those returned by the regular Map methods, to be used as the target for a for await..of loop. This allows you to write code such as the above loop that will work with either a Map or AsyncMap.

API #

static async groupBy(iterable, keyFn)

  • iterable: any
  • keyFn: (element: any, index: any) => Promise

Returns: Promise

Groups items from an async iterable into an AsyncMap according to the keys returned by the given function.

async clear()

Returns: any

Remove all key/value entries from the map.

This method invokes the keys() and delete() methods.

async delete(key)

  • key: any

Returns: Promise

Removes the entry for the given key, return true if an entry was removed and false if there was no entry for the key.

*entries()

Returns: AsyncIterableIterator<[any, any]>

Returns a new AsyncIterator object that contains a two-member array of [key, value] for each element in the map in insertion order.

This method invokes the keys() and get() methods.

async forEach(callback, [thisArg])

  • callback: (value: any, key: any, thisArg: any) => Promise
  • thisArg: any

Returns: any

Calls callback once for each key/value pair in the map, in insertion order.

This method invokes the entries() method.

async get(key)

  • key: any

Returns: Promise

Returns the value associated with the key, or undefined if there is none.

async has(key)

  • key: any

Returns: unknown

Returns true if the given key appears in the set returned by keys().

It doesn’t matter whether the value returned by get() is defined or not.

If the requested key has a trailing slash but has no associated value, but the alternate form with a slash does appear, this returns true.

*keys()

Returns: AsyncIterableIterator

Returns a new AsyncIterator object that contains the keys for each element in the map in insertion order.

parent

Type: AsyncMap

The parent of this node in a tree.

readOnly

Type: boolean

True if the object is read-only. This will be true if get() has been overridden but set() and delete() have not.

async set(key, value)

  • key: any
  • value: any

Returns: Promise

Sets the value for the given key.

size

Type: Promise

Returns the number of keys in the map.

The size property invokes an overridden keys() to ensure proper behavior in subclasses. Because a subclass may not enforce a direct correspondence between keys() and get(), the size may not reflect the number of values that can be retrieved.

*values()

Returns: AsyncIterableIterator

Returns a new AsyncIterator object that contains the values for each element in the map in insertion order.