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

283
Vistas
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 Respuestas
Responde la pregunta

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 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