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

179
Visualizações
How to stop function until the invoked function is done

I have some AngularJS controllers that create graphs from a database data. Before the data is process and the graph render I have to invoke the function GetSensor(x,y), this fucntion return an Id in order to ask for the right data to the database

My problem is that GetSensor(x,y) returns the data way too late. The main function is doing the request with the promise on pending.

I´ve used .then() but still does not wait for the promise to be resolved.

GetSensor():

async function getSensores(Idinfraestrucura, TipoSensorId) {
console.log("whassup")
var xmlhttp = new XMLHttpRequest();
var url = "http://M.Y.I.P:PORT/api/sensores/" + TipoSensorId + "/" + Idinfraestrucura + "";
xmlhttp.open("GET", url, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
        var data = JSON.parse(this.responseText)
        console.log(data[0].SigfoxDevice)
        return data[0].SigfoxDevice;
}
}

GraphController():

app.controller('HumedadController', ['$scope',
function ($scope) {

    let sensor = getSensores(9, 1)
        .then(function (response) {
            console.log(sensor)                       
            var xmlhttp = new XMLHttpRequest();
            var url = "http://My.Y.I.P:PORT/api/grafico/datos/ValorMedicionHumedad/MedicionAire/" + sensor + "";
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
            xmlhttp.onreadystatechange = function () {
                if (this.readyState == 4 && this.status == 200) {
                    var dataURL = JSON.parse(this.responseText);
                    var dataPoints = dataURL.map(function (elemF) {
                        return elemF.map(function (o) {
                            return {
                                fecha: o.fechaUnix,
                                Y: o.ValorMedicionHumedad
                            }
                        })
                    });
.
.
.Rest of the code is .render and data transformation

                

Since I was in a hurry and needed this to work, I had not time to get deep into AngularJS and has to be done this way, so I just created the controllers for the graphs. May I have getSensores() in, for example, a .service or somthing like that?

Sorry If this is a basic problem that could be solved with a deeper understanding of AngularJS.

about 4 years ago · Juan Pablo Isaza
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