Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add async versions of callback methods #114

Open
david-morris opened this issue May 17, 2024 · 0 comments
Open

add async versions of callback methods #114

david-morris opened this issue May 17, 2024 · 0 comments

Comments

@david-morris
Copy link

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(...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant