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

127
Visualizações
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 Respostas
Responde à pergunta

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