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

292
Visualizações
How to get callback data outside of a function (AWS SDK)(non async)

I have a function that gets data from AWS:

main.js

const { GetInstancesByName } = require("./functions");


var operationmode = "getinstances";

if (operationmode == "getinstances") {
  let getresult = GetInstancesByName(instance);
  console.log("(getinstances log)",getresult);
  let resultsent = "yes";
  callback(getresult);
}

then this is the functions file (functions.js):

functions.js

const GetInstancesByName = function GetInstancesByName(name) {
    
  let ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
  //console.log(ec2);

  let params = {
    Filters: [
      {
        Name: "tag:Name", 
        Values: [
          name
        ]
      }
    ]
  };
 
  ec2.describeInstances(params, function(err, data) {
    if (err) 
      console.log("error",err, err.stack); // an error occurred
    else return data;                      // successful response
      /*
      data = {
      }
      */
  });

  return data;
};

I am trying to get the data from the (error , data) in the function. I have tried: setting a var/const/let to the data. the return data at the bottom works but data is empty. I want to get data so I can pass it back in the GetInstancesByName (being used by main.js).

on the main both console.log("(getinstances log)",getresult) and callback(getresult); both return not defined.

Any help would be much appreciated.

Thank You

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

0

If you don't want to use async/await, you may pass a callback to GetInstancesByName as follows:

function GetInstancesByName (name, callback) {

    // ...

    ec2.describeInstances(params, function(err, data) {
        if (err) { 
            console.log("error",err, err.stack); // an error occurred
        } else {
            callback(data);                      // successful response
        }
    });

    // ...
}

// In main.js
GetInstancesByName(instance, data => { 
    // do something with data
});
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