I'm trying to create a function in Firebase Functions that returns a promise (or returns synchronously, I don't mind), but with no success.
Here's the function that I wrote:
function doSomethingLong(param) {
https.get('http://www.myurl.com?param=' + param, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log("Call succeeded. Response: " + data);
return true;
});
}).on("error", (err) => {
console.log("Call failed. Error: " + err.message);
return false;
});
}
I want to call it when a certain change in Firebase occurs, and wait till it completes, something like:
exports.someFunction = functions.database.ref('/users/{user_id}/param').onCreate(event => {
const param = event.data.val();
if (doSomethingLong(param)) {
console.log("ttt");
} else {
console.log("fff");
}
return null;
})
No matter what I try, the someFunction
function ends before doSomethingLong
ends.
Any clues?