How to use async function as parameter in TypeScript

Issue #640

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
async function useCache(
collectionName: string,
key: string,
response: functions.Response<any>,
fetch: () => Promise<any>
): Promise<any> {
const existing = await db.collection(collectionName).doc(key).get()
if (existing.exists) {
response.send(existing.data())
return
}

const object = await fetch()
const json = Object.assign({}, object)
await db.collection(collectionName).doc(key).set(json)
response.send(object)
}
1
2
3
4
5
6
7
8
9
useCache(
"books",
key,
response,
async () => {
const service = new Service()
return await service.doSomething(key)
}
)

Comments