Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

470
Vistas
wait for return function value before callback triggered

I am trying to create a module that fetches all the info one by one and stores it in an array at the end it returns the array, instead of the loop I am using recursion. The problem here is that my main function is not waiting for the return value due to which no value is passed in the callback. I can re-write this async await or promise but if the same type of problem is present in the entire project I don't want to re-write the entire thing. This can be handled by callbacks? or some other way I can resolve it.

    const GetRequest = require("./getRequest.js");
module.exports = function GetProcessList(environment, token, processCompleteList, callback) {
    var allProcessDraft = [];
    var loopMaxRun = processCompleteList.data.length -1;
    var a = RecursiveGet(environment, token, processCompleteList, allProcessDraft, loopMaxRun);
    callback(a); //Not waiting for the value
}

//recursion function to get all the data one by one
function RecursiveGet(environment, token, processCompleteList, allProcessDraft, index) {
    var URL = "https://" + environment + "...../rest/process/versionPage?calledFrom=master&pageNo=0&processMasterId=" + processCompleteList.data[index].id + "&recordsPerPage=10&sortType=DESC";
    if (index != 0) {
        GetRequest(URL, token, function (processDraft) {
            allProcessDraft.push(processDraft);
            return RecursiveGet(environment, token, processCompleteList,allProcessDraft, index -1);
        });
    } else { 
        return allProcessDraft;
    }
 }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try the below code.

async function RecursiveGet(environment, token, processCompleteList, allProcessDraft, index) {
    var URL = "https://" + environment + "...../rest/process/versionPage?calledFrom=master&pageNo=0&processMasterId=" + processCompleteList.data[index].id + "&recordsPerPage=10&sortType=DESC";
    if (index != 0) {
       const processedDraft = await GetRequest(URL, token);
       allProcessDraft.push(processDraft);
       return RecursiveGet(environment, token, processCompleteList,allProcessDraft, index -1);
    } else { 
        return allProcessDraft;
    }
 }

note: make sure GetRequest is async

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda