Why is this async function returning empty on the first try, but then it returns everything?

le0nicolas

I'm trying to insert data to my db, and the query is inside a map loop, so i'm using an async function to wait for the loop to end, and store all the results in my variable "practicasAgregadas".

This is how i call the function:

insertarPracticas(turno_id, req.body.lista_codigos_practicas, queryInsertarPracticas)
  .then(result => {
    res.status(200).json({
      "Practicas Agregadas": result
    })
  })

This is the function:

async function insertarPracticas(turno_id, req, queryInsertarPracticas) {
  const res = await Promise.all(req.map(r => {
    connection.query(
      queryInsertarPracticas, [turno_id, r], (error2, row2) => {
        if (error2) {
          console.log("Error al insertar turno detalle (prácticas): " + r + " " + error2);
          practicasNoAgregadas += r + "-";
        } else {
          console.log("Turnos detalle agregados " + r);
          practicasAgregadas += r + "-";
          console.log("practicas " + practicasAgregadas);
        }
      });
    return practicasAgregadas;
  })
  )

  console.log("en async " + res[0]);
  return res;
}

On the first try, it returns empty:

enter image description here

This is the console:

enter image description here

And on the second try, it does return, but it repeats 3 times:

enter image description here

And the console:

enter image description here

Keith

Most modern JS libs that use async functions will return a promise, but will often not return a promise if the callback is supplied.

So assuming connection.query does return a promise, then the code below should do what your after..

async function insertarPracticas(turno_id, req, queryInsertarPracticas) {
    const res = await Promise.all(
        req.map(async (r) => {
            try {
                const row2 = await connection.query(queryInsertarPracticas, [
                    turno_id, r
                ]);
                console.log("Turnos detalle agregados " + r);
                practicasAgregadas += r + "-";
                console.log("practicas " + practicasAgregadas);
            } catch (e) {
                console.log(
                    "Error al insertar turno detalle (prácticas): " +
                        r + " " + error2
                );
                practicasNoAgregadas += r + "-";
            }
            return practicasAgregadas;
        })
    );

    console.log("en async " + res[0]);
    return res;
}

Remember, don't use a callback, or that could indicate to the lib that your not wanting a promise returning.

If for some reason the lib has not been updated for a long while, you will need to promisify the query..

In node, there is a nice utility called promisify https://nodejs.org/api/util.html#utilpromisifyoriginal that will make this bit easier.

eg.

const conQuery = util.promisify(connection.query);
const row2 = await conQuery(queryInsertarPracticas....

If the callback is not in the form (error, result) you would need to use a Promise constructor.. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise , but looking at your code (error2, row2) => that shouldn't be required.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

HTTPS Callable Function that returns async function returning empty object

Why my async function returns an undefined when i try to access it?

Why the below function is not returning any value and it is always returns empty

Async function returns empty array

Why React returns empty when calling a function for the first time?

Why is this function returning an empty vector?

Why is this function returning an empty list?

Async function returning empty value then doing operations?

Why is this function returns an empty array?

Why Async function in Next JS returns empty object in my Typescript code?

Why is my async function returning too soon?

Why is try {} .. catch() not working with async/await function?

Why is my function returning an empty string?

Why is my recursive function returning an empty list?

Why my function is returning empty string in python?

Why is my function returning an empty array?

Async function returning empty array, and then the desired data - causing app to crash

Why returning values from sqlite returns an empty array

If everything in javascript is object then why typeof operator returns function for object?

Context with React Native hooks returns empty object/array on async function

Why is my function returning 0 after a try-except clause?

Why the first time that i use async it will give me an empty array?

Why isn't Javascript async function immediately returning?

Execute function depending on which async call returns first

Async function returning undefined

Async function is returning undefined

Why does my function keep returning an empty list?

Why SQL function always returning empty result set?

Why my state is empty when I try to access it from a function?