最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: write function that returns a promise - Stack Overflow

programmeradmin1浏览0评论

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('=' + 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 pletes, 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?

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.?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 pletes, 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?

Share Improve this question edited Dec 2, 2017 at 13:36 Slavik N asked Dec 2, 2017 at 13:18 Slavik NSlavik N 5,36820 silver badges25 bronze badges 4
  • 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 Commented Dec 2, 2017 at 13:24
  • If I am not mistaken, this is what you are trying to achieve: pastebin./hS5Bt8tY – Kostas Minaidis 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 plete, and only then return. – Slavik N Commented Dec 2, 2017 at 13:38
  • Possible duplicate of How do I convert an existing callback API to promises? – André Werlang Commented Dec 2, 2017 at 13:43
Add a ment  | 

3 Answers 3

Reset to default 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.?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)
   }

)

Do something like,

    function doSomethingLong(param) {
    return new Promise(function(resolve,reject){
        https.get('http://www.myurl.?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);
    })
})

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

发布评论

评论列表(0)

  1. 暂无评论