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

285
Visualizações
How to return a value from a promise which isn't a pending promise: d3.js, Javascript?

I am relatively new to asynchronous javascript programming. When using the d3.scv function from the d3 library, I ran into an issue where when awaiting the results of the promise from d3.csv inside a async function scope, I can directly access the resolved value, but when I return this resolved value I get a pending promise.

Code

async function generateCountryData(){
        let csvData = await d3.csv("./data/data.csv", (d) => {
                return {
                        "id" : +d["id"], // The '+' turns a string into a number
                        "country" : d["name"],
                        "faction" : +d["overlord"]
                }
        });

        let dataArray = csvData.map(Object.values);

        // LOG 1
        console.log(dataArray);
        return dataArray;
}
// LOG 1: Array(58)
// Returns: Promise {<pending>} "fulfilled"[[PromiseResult]]: Array(58)

async function func1() {
    let result = await generateCountryData();

    // LOG 2
    console.log(result);
    return result;
}
// LOG 2: Array(58)
// Returns: Promise {<pending>} "fulfilled"[[PromiseResult]]: Array(58)

Since I dont want to define a new async function each time I want to access this array, I want to know how I can return a defined resolved value which isn't a pending promise.

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

0

It's annoying, but this is how it works. Anything that needs access to the result of an asynchronous operation must itself be asynchronous.

So while there's different patterns for dealing with this (callbacks, observers, promises), you can't get around this so best to embrace it.

You can't turn async code into non-async code. An interesting article about this:

https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/

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