Promise inside Premomise does not wait Axios

Asked

Viewed 119 times

0

Eae guys I’m having a problem, I get several objects where I mapped them, make an external query using the Axios and save your return. Let’s go to code:

let savedClients = Object.entries(documents).map(personDocument => {
    let [person, document] = personDocument
    documentFormated = document
    documentNumbers = document.replace(/\D/g, '')

    return ConsultDocuments.getResponse(documentNumbers).then(resultScore => { // Chamando o axios
        const info = { ...resultScore }
        return Save.saveClient(info)
    })
})

Promise.all(savedClients).then(results => {
    console.log(results) // Vem só o ultimo objeto salvo varias vezes
})

poblema is that it performs the entire map first, then queries with the last result several times (the total of documents passed)

This code is a legacy and using async/await does not work (seriously, I wouldn’t even be here)

I’ve tried N forms, and with the Q library, it does in the right order but does not wait for the Xios, the results come all with pending

Thanks.

  • Can inform the contents of the variable documents?

  • { name: Cpf } example, { fulano : 12312312312398 }

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.