Problem with functions not waiting for variable to be filled before progressing

37 views Asked by At

Soo i am developing this app, and i cant seem to figure how to fix this issue that i am having,

the problem is that the first function is always throwing undefined on the "console.log(params)", i assume the problem is because the "const result" is not waiting for the promise to end filling all the data before being passed on.

What i need is some help figuring out how to make the rest wait for result to be field before moving on.

    export async function registar_compra(compra){

    const params = await create_params(compra)
    
    console.log(params)
}

async function create_params(compra) {
  
const {numero_socio, nome_tuna, nome_completo, produtos_selecionados} = compra

const result =  {
                RequestItems: {
                    ['HISTORICO']: produtos_selecionados.map(async (produto, index) => ({
                    PutRequest: {
                        Item: {
                            id: "compra",
                            data_dia: await get_data(index),
                            numero_socio: numero_socio,
                            nome_tuna: nome_tuna,
                            nome_completo: nome_completo,
                            quantidade: produto.quantidade,
                            nome_produto: produto.nome_produto,
                            preco_quantidade: produto.preço_quantidade
                        },
                    },
                    })),
                },  
            }

return result

}

function get_data(index) {
    return new Promise(resolve=> {
      setTimeout(() => {
        resolve(new Date().toISOString());
      }, 12 * index);
    });
 }

I have tryed debugging it too see where the problem is, just cant seem to find the way to make this work

0

There are 0 answers