1

I am new to async / await and promises and such in JS. I am trying to build a small bit of software which actually seems to work. However, one function doesn't finish and seems to run forever without returning a result. Here is my code:

module.exports.visit = (val) => {

        let visit_date = Date.now();
        let data_json;
        let insert;

        client.get(val)
        .then( (data) => {
                data_json = JSON.parse(data);
                data_json.count += 1;
                data_json.last_visited = visit_date;
                insert  = JSON.stringify(data_json);
                client.set(val,insert);
                console.log(insert);
                return data_json.url;
        })
        .catch((err) => {
                console.log(err);
        });
}

The function calling this function (in order to return the url):

async function visitor() {
        // also doesn't work with let a = await helpers.visit('6q9ootm8p7');
        let a = helpers.visit('6q9ootm8p7');
        return a;
        }

visitor();

I would be very thankful if somebody could point me into the same direction.

1 Answer 1

1

helpers.visit must return a promise. Currently, it is void. Try:

return client.get(val)
  .then(() => {
    // provide your code here, and return the data
    return ... ;
  })
  .catch(error => {
    console.error(error);

    // if you catch an error, you should provide your result data too
    return ... ;
  })
;
Sign up to request clarification or add additional context in comments.

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.