1

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?

Slavik N
  • 4,705
  • 17
  • 23
  • So, what you want is to run doSomethingLong and then once the resp.on('end') fires, pass the result 'data' to someFunction, right? – Kostas Minaidis Dec 02 '17 at 13:24
  • If I am not mistaken, this is what you are trying to achieve: https://pastebin.com/hS5Bt8tY – Kostas Minaidis Dec 02 '17 at 13:34
  • (Fixed a few misleading typos in the original question). @KostasX, what I'm trying to achieve is: when someFunction is called, it should call doSomethingLong, wait for it to complete, and only then return. – Slavik N Dec 02 '17 at 13:38
  • Possible duplicate of [How do I convert an existing callback API to promises?](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) – André Werlang Dec 02 '17 at 13:43

3 Answers3

4

You have to create a Promise and resolve/reject it when your asynchronous task ends.

function doSomethingLong(param) {

   return new Promise(function(resolve, reject){

       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);
              resolve();
          });
      }).on("error", (err) => {
          console.log("Call failed. Error: " + err.message);
          reject(err);
      });
   });
}

Now you can use it like this

doSomethingLong()
   .then(function() {
      console.log(pased)
   },

   function(err) {
     console.log(err)
   }

)
Charlie
  • 22,886
  • 11
  • 59
  • 90
0

Do something like,

    function doSomethingLong(param) {
    return new Promise(function(resolve,reject){
        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);
                resolve(data);//resolve data
            });
        }).on("error", (err) => {
            console.log("Call failed. Error: " + err.message);
            reject(err);//reject(reason)
        });
    });
}


exports.someFunction = functions.database.ref('/users/{user_id}/param').onCreate(event => {
    const param = event.data.val();

    if (addUserToNewsletter(user_id, name, email, language)) {
        console.log("ttt");
    } else {
        console.log("fff");
    }
    return null;
    let promise=doSomethingLong('foo');
    promise.then((data)=>{
        console.log(data);
    }).catch((err)=>{
        console.log(err);
    })
})
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
0

You could join them both up by returning a promise, promise.resolve or promise.reject.

Making your code look something like this:

 this.props.doSomethingLong(loginResult.credential)
          .then(result => this.someFunction(result))
          .catch(error => this.handleError(error.message))

doing this:

exports.someFunction = functions.database.ref('/users/{user_id}/param').onCreate(event => {
const param = event.data.val();

if (addUserToNewsletter(user_id, name, email, language)) {
    console.log("ttt");
} else {
    console.log("fff");
}
return Promise.resolve();

})

doing this each function call, you can read more about Promises and how they work in Node-js

Aaqib
  • 9,942
  • 4
  • 21
  • 30
Stephen
  • 889
  • 8
  • 18