I am using Firebase Functions. Basically, an API corresponds to one function. So, I must return the result within the function. See the pseudocode below.
export const someAPI = functions.https.onRequest((...) =>{
parameter = get the parameter from the request
data = searchDatabase(parameter)
if data is not null
do something and create a result
else
do something else and create a different result
send the result as response.
});
I have created the searchDatabase
function, and it works. The problem is, the query result is retrieved by a callback.
function searchDatabase(token:string):string
{
const db = admin.database().ref("mydata");
db.....once("value",
function(snapshot){
snapshot.forEach(
function(data)
{
const deviceId = data.val().id;
return false;
}
);
}
)
return ?; <- I need to return `deviceId` at this point.
}
I have read this answer, and I would like to take "the blue pill", but that answer was for Java and it seems that there is no semaphore
for TypeScript. Can I do the similar thing in TypeScript?
After reading the comment, I decided to use async/wait. I do not know how to make the API function itself an async function, so I did this.
export const someAPI = functions.https.onRequest((request, response) =>{
realFunction(request, response);
});
async function realFunction(request: express.Request, response: express.Response)
{
parameter = get the parameter from the request
data = await searchDatabase(parameter)
if data is not null
do something and create a result
else
do something else and create a different result
send the result as response.
}
and I made the searchDatabase
function return a Promise.
async function searchDatabase(token:string): Promise<string>
{
return new Promise<string>((resolve, reject) => {
const db = admin.database().ref("mydata");
db.orderByChild("..").equalTo(token).once("value")
.then(function(snapshot){
snapshot.forEach(
function(data)
{
//match found, but what about all other cases?
const deviceId = data.val().id;
resolve(deviceId);
return false;
}
);
resolve(null);
})
.catch(reason=>{resolve(null);})
});
}