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

126
Vistas
Persisting data beyond the lifetime of a callback function

Forgive me if this is a commonly asked question, I'm new to JavaScript.

I'm attempting to use the library "video-url-link" from npmjs.

The Readme provides the following example:

videoUrlLink.twitter.getInfo('https://twitter.com/{@}/status/{ID}', {}, (error, info) => {
    if (error) {
        console.error(error);
    } else {
        console.log(info.full_text);
        console.log(info.variants);
    }
});

This works fine, but what are my options if I want the "info" object to persist outside of the scope of the callback?

I've tried declaring a variable outside the getInfo call, then setting it inside the callback, which I didn't expect to work - and it doesn't work(!)

What I have is:

const get_twitter_video_info = (video_url) => {

    var nfo = {};

    try
    {
        videoUrlLink.twitter.getInfo(
            video_url,
            {},
            (error, video_info) => {

                if (error !== undefined)
                    console.log(error);

                if (video_info !== undefined)
                {
                    nfo = JSON.parse(JSON.stringify(video_info));
                }
            }
        );
    }
    catch(err)
    {
        console.log (err);
        throw(err);
    }

    return nfo;
}

thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are attempting return nfo before the callback is even called. In the code, get_twitter_video_info will return an empty object and then getInfo will return the response and the callback will be called (where you are using nfo and video_info). You can either utilise nfo in the callback itself and process further or you can use async/await.

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