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

149
Visualizações
Best way to handle api callbacks in wordpos library?

I'm new handling promises in javascript and I've found a problem with this node js lib https://www.npmjs.com/package/wordpos I'm trying to find all the verbs that are found inside an arrays of strings, and for this I've created this function:

function calculateVerbsAmount(stringsArray) {
return stringsArray.map(function(item) { 
    let element = wordpos.getVerbs(item, function(result){
        return result;
    });    
});

}

What I expect is to have a new array, this time of verb strings, but since the lib handles the methods as callbacks I'm getting

[Promise { <pending> }, Promise { <pending> },
Promise { <pending> }, Promise { <pending> },
Promise { <pending> }, Promise { <pending> }]

I've tried added async/await but they always return the same result, any idea on how to do some synchronous call to the wordpos.getVerbs method?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Calling wordpos.getVerbs() returns a promise. Since you are doing this inside a .map function your stringsArray will be an array of promises (as you actually get as return.).

A single Promise can be unwraped using await. For an array of Promises you can use Promise.all helper which allows waiting for each Promise within an array.

Example

async function calculateVerbsAmount(stringsArray) {
    const verbPromises = stringsArray.map(function(item) { 
        let element = wordpos.getVerbs(item, function(result){
            return result;
        });    
    });

    // await every Promise within the verbPromises array. Promise.all
    // returns a single Promise which holds an array with the native values
    return Promise.all(verbPromises)
}


// the keyword "await" can only be used within an async function. 
// So for showcasing it i created the main() function ..
async function main() {
    
    // Now call the calculateVerbsAmount() and await it for receiving the expected
    // array of strings. 
    const verbs = await calculateVerbsAmount()
}


about 4 years ago · Juan Pablo Isaza 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