Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
async/promise with multiple api calls

I kind of understand the differences between callbacks, promises and async await, but I'm not quite sure how to apply this to my problem.

I stripped away a lot of the code, but basically I have an app running with an endpoint that needs to execute 3 functions (maybe I need a 4th?) and also send a "res.end()" within 3 seconds. The 3 functions are dependent on each other. Do I need to chain these functions? My main() seems completely wrong.

  • deleteRule() has to run and finish first
  • createRule() must run after deleteRule() has completed
  • orderRule() must run after createRule() has completed
router.post('/', function (req, res) {

        async function deleteRule() {
            axios.delete(DeleteURL, {auth: auth, httpsAgent: agent})
            .then(response => {
                let deleteRes = response.data
                console.log(deleteRes)
            })
        }

        const list = './jsons/list.json'

        async function createRule() {
            fs.readFile(list, 'utf-8', function(err, data) {
                if (err) throw err
                var thestuff = JSON.parse(data)
            
            axios.post(CreateURL, thestuff, {auth: auth, httpsAgent: agent})
            .then(response => {
                let createRes = response.data
                console.log(createRes)
            })
        })
        }

        async function orderRule() {
            axios.put(usOrderURL, theOrder, {auth: auth, httpsAgent: agent})
            .then(response => {
                let orderRes = response.data
                console.log(orderRes)
            })
        }

        async function main() {
            const deleteListResult = await deleteRule();
            const createListResult = await createRule();
            const orderListResult = await orderRule();
        }

        main();

        // res.end must finish in 3 seconds from the initial post on the first line
        res.end() 

    })
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The then() calls return promises, but you don't do anything with them. You should either await them, or return them.

Since you declared your functions as async, make use of await in them -- that is the whole point of the async keyword:

async function deleteRule() {
    let response = await axios.delete(DeleteURL, {auth: auth, httpsAgent: agent});
    console.log(response.data);
    return response.data;
}

Make a similar change to the other two rule-functions.

over 4 years ago · Santiago Trujillo Relatório

0

import fs from 'fs-extra'

const list = './jsons/list.json'

async function deleteRule() {
    const response = await axios.delete(DeleteURL, {auth: auth, httpsAgent: agent})
    const deleteRes = response.data

    console.log(deleteRes)

    return deleteRes
}
async function createRule() {
    const data = await fs.readFile(list, 'utf-8')
    const theStuff = JSON.parse(data)
    const response = await axios.post(CreateURL, theStuff, {auth: auth, httpsAgent: agent})
    const createRes = response.data
    
    console.log(createRes)

    return createRes
}
async function orderRule() {
    const response = await axios.put(usOrderURL, theOrder, {auth: auth, httpsAgent: agent})
    const orderRes = response.data
    
    console.log(orderRes)

    return orderRes
}

router.post('/', async function (req, res) {
    const deleteListResult = await deleteRule();
    const createListResult = await createRule();
    const orderListResult = await orderRule();

    // res.end must finish in 3 seconds from the initial post on the first line
    res.end() 
})
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda