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?

4
  • So, what you want is to run doSomethingLong and then once the resp.on('end') fires, pass the result 'data' to someFunction, right? Commented Dec 2, 2017 at 13:24
  • If I am not mistaken, this is what you are trying to achieve: pastebin.com/hS5Bt8tY Commented Dec 2, 2017 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. Commented Dec 2, 2017 at 13:38
  • Possible duplicate of How do I convert an existing callback API to promises? Commented Dec 2, 2017 at 13:43

3 Answers 3

6

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)
   }

)
Sign up to request clarification or add additional context in comments.

Comments

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);
    })
})

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.