-
Notifications
You must be signed in to change notification settings - Fork 84
Open
Description
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
Labels
No labels