0

I need to wait for queries from a database I have in Postgres from a function that uses a callback.

I have a function to get rows from the database (queries.js):

const getRecipesByCategoryForSection = (callback, category) => {    

    pool.query("SELECT * FROM recipes WHERE category=$1 ORDER BY RANDOM() LIMIT 10;", [category], (error, results) => {

        if (error) {
            console.log(error);
            throw error;
        }
        callback(results.rows);
    })
}

As you can see, I am using a callback function to get the rows from the database. I want to use those rows to display them on one page, but displaying various categories. I use it in my server.js in the following manner:

app.get("/recipes", function (req, res) {
        var breakfasts = [];
        var lunches = [];
        var desserts = [];

        db.getRecipesByCategoryForSection(function (rows) {            
            breakfasts = rows;      
        }, 'breakfast');


         db.getRecipesByCategoryForSection(function (rows) {
             lunches = rows;
         }, 'lunch');

         db.getRecipesByCategoryForSection(function (rows) {
             desserts = rows;
         }, 'snack');


        res.render("recipes", {
            breakfasts: breakfasts,
            lunches: lunches,
            snacks: snacks
        });
});

But in this configuration, variables breakfasts, lunches, and desserts contain, of course, nothing.

How do I set the function in the queries.js in respect to callbacks so the function in server.js would wait for the rows before executing the rest of the code?

I would be grateful for any help, I am quite new to this so any explanation and help would be very valuable. Thank you.

Marta P
  • 13
  • 2
  • [Convert the API to uses promises](https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) and then use `Promise.all` to run them concurrently. – Quentin Jul 02 '19 at 10:18
  • Thanks, although I see only half of the answer to my question in this, supposedly, duplicate question. I still don't know how to use it. – Marta P Jul 02 '19 at 12:37

1 Answers1

-1

You will need to include the function invocations inside the callback functions as below which will solve the issue. But this is not recommended as it leads to callback hell where could is difficult to read. Ideally you should change the coding style to async/await so that the code will be clean.

app.get("/recipes", function (req, res) {
    var breakfasts = [];
    var lunches = [];
    var desserts = [];

    db.getRecipesByCategoryForSection(function (rows) {            
        breakfasts = rows;
        db.getRecipesByCategoryForSection(function (rows) {
            lunches = rows;
            db.getRecipesByCategoryForSection(function (rows) {
                desserts = rows;
                res.render("recipes", {
                    breakfasts: breakfasts,
                    lunches: lunches,
                    snacks: snacks
                });
            }, 'snack');
        }, 'lunch');      
    }, 'breakfast');    
});
Boney
  • 1,712
  • 2
  • 14
  • 22