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

169
Visualizações
I am using async, await, Promise in JavaScript but the results are not sent to response in Express.js

I am new to nodejs(Express.js and JavaScript) so kindly excuse me. I have the following code

function lookUpServiceProcessor(element){
    return new Promise((resolve, reject) => {
        const lookUpService = new LookUpService();
        const data = lookUpService.process(element);
        resolve(data);
        reject(data);
    })
}

function keywordServiceProcessor(element){
    return new Promise((resolve, reject) => {
        const keywordService = new KeywordService(process.env.KEYWORD_SERVICE_HOSTNAME, 
            process.env.KEYWORD_SERVICE_PORT, process.env.KEYWORD_SERVICE_ENDPOINT);
        const data =  keywordService.process(element);
        resolve(data);
        reject(data);
    })
}

function patternServiceProcessor(element){
    return new Promise((resolve, reject) => {
        const patternService = new PatternService(process.env.PATTERN_SERVICE_HOSTNAME, 
            process.env.PATTERN_SERVICE_PORT, process.env.PATTERN_SERVICE_ENDPOINT);
        const data = patternService.process(element);
        resolve(data);
        reject(data);
    })
}

const processEachDocument = (document) => {
     return Promise.all([ 
                lookUpServiceProcessor(document),
                keywordServiceProcessor(document),
                patternServiceProcessor(document)
            ]);
}

router.post('/extraction', (req, res) => { 
    result = [];         
    req.body.map(document => {
            processEachDocument(document)
        .then(response => result.push(response))
        .catch(error => result.push(error));
    });   
    res.send(result); 
})

Problem :

  1. Nothing is populated in result array. I understand that nodejs spins up a background thread (i.e like reactive programming) to process Promise and we need async and await for the results to appear.
  2. Hence I tried below code with async and await but result array does not have anything.
  3. In both the cases when I console.log they get populated. I read couple of stackoverflow posts and I understand that I need to have the async and await in the proper places. Since I am new I am not getting where I need to have them to get the proper response sent out.

Any leads are much appreciated. Thanks

router.post('/extraction', (req, res) => { 
    result = [];       
    req.body.map(document => {
         (async () => {
             const processedDocument = await processEachDocument(document);
             await result.push(processedDocument);
         })();   
        
    });   
    res.send(result); 
}) 
about 4 years ago · Juan Pablo Isaza
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