Skip to content

add async versions of callback methods #114

@david-morris

Description

@david-morris

Async is often an easier to use abstraction for delayed computation than callbacks.

As a result, I've been writing code like this:

  const info = await new Promise<GetInfoCallbackResult>((resolve, reject) => {
    tiles.getInfo((error, info) => {
      if (error) {
        // eslint-disable-next-line no-console
        console.log(
          "failed to get tiles: ",
          TILES_PATH,
          " from ",
          process.cwd()
        )

        reject(error)
      } else {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        resolve(info!)
      }
    })
  })

It would be nice if that became something like tiles.agetInfo(...).

It's also useful to await the constructor making things ready:

const TILES_OBJECT_PROMISE_CACHE = new Map<string, Promise<MBTiles>>()

export const getTileObject = async (uri: string): Promise<MBTiles> => {
  const cached = TILES_OBJECT_PROMISE_CACHE.get(uri)
  if (cached) return await cached
  const tilesObjectPromise = new Promise<MBTiles>((resolve, reject) => {
    const mbtiles = new MBTiles(uri, error => {
      if (error) {
        reject(error)
      } else {
        resolve(mbtiles)
      }
    })
  })
  TILES_OBJECT_PROMISE_CACHE.set(uri, tilesObjectPromise)
  return await tilesObjectPromise
}

It would be neat if the part where you get a promise was something like MBtiles.ABuildMBTiles(...).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions