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

94
Visualizações
Creating a Generalized Helper Function that Promisifies Functions and Resolves with Returned Value

I am trying to create a helper function that will promisify a function that takes an arbitrary number of arguments. Intuitively, the problem might be tackled by wrapping resolve with callbacks, which I've always struggled with, so please tell me explicitly how do so. Thanks.

function prom(rawFunc, ...arg){
    // This is the helper function
    return new Promise ((resolve, reject)=>{
        checkFn(rawFunc);
        function checkFn(rawFunc){
            if (rawFunc instanceof Function === false){
                return reject("Error: rawFunc passed is not a function");
            } 
            let returnValue = rawFunc(...arg)
            resolve(returnValue);
        }
    });
}

function targetFn1 (a, b){
    console.log(`targetFn1 should return ${a+b}`)
    setTimeout(() => {
        console.log(`truly returning value ${a+b}`)
        return a + b 
    }, 4000);
    // this function returns a+b after a delay of 4 seconds
}

function targetFn2 (c, d, e){
    console.log(c)
    console.log(d)
    console.log(e)
    console.log(`targetFn2 returning ${c+d+e}`)
    return c+d+e
}

async function mainFn(){
    let value1 = await prom(targetFn1,1,3)
    let value2 = await prom(targetFn2,value1,3,4);
}

mainFn();


// console log :
// targetFn1 should return 4
// undefined
// 3
// 4
// targetFn2 returning NaN
// truly returning value 4
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