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

412
Visualizações
AngularJS v1: get value of promise

We are using an ancient (1.x) version of AngularJS. I am trying to use the outcome of a promise - but I can't seem to get the value. In the example I am providing here - you can see that I even tried using the global / window scope to store and later retrieve the value.

In previous versions I have tried var xxx and $scope.xxx, to the same result: I can't seem to access the content of my promise.

My code;

angular.module('surveyBuilder').controller('cloneUsersCtrl', cloneUsersCtrlFunction);
cloneUsersCtrlFunction.$inject = ['$http','$scope', '$state', '$stateParams', '$cookies', 'TenantHandler', 'Survey', 'TENANT', 'PERMISSION', 'Notification', '$log'];
function cloneUsersCtrlFunction($http, $scope, $state, $stateParams, $cookies, TenantHandler, Survey, TENANT, PERMISSION, Notification, $log) {
    $scope.surveyId = $stateParams.surveyId; 
    $scope.selectedTenant = '' + TenantHandler.getSelected();
    $scope.canEdit = PERMISSION.edit;
    $scope.activeOnly = false;
    
    var getDatasources = function () {
        return $http.get(apiBase + 'tenants/' + $scope.selectedTenant + '/surveys/' + $scope.surveyId + '/cloneUsers');
    }

    getDatasources().then(function(data) {
        console.log('data.data: line 439:  ' + data.data);
        // Use the global WINDOW scope
        window.gavinData = data.data;
        console.log('window.gavinData: Line 442: ' + window.gavinData); 
    });
    console.log(window);
    console.log('window.gavinData: Line 445:' + window.gavinData);

And here is the console's output.

app.js:444 Window {... //clipped for succintness

app.js:445 window.gavinData: Line 445:undefined

app.js:439 data.data: line 439:  362,CUSTOMERFIRSTBANK daily email recipient data,363,Retail Banking Survey,371,Medibank Survey,383,Copy of Frequent Flyer Card Survey,404,DeplyTest,405,Petbarn Survey,406,Copy of Petbarn Survey

app.js:442 window.gavinData: Line 442: 362,CUSTOMERFIRSTBANK daily email recipient data,363,Retail Banking Survey,371,Medibank Survey,383,Copy of Frequent Flyer Card Survey,404,DeplyTest,405,Petbarn Survey,406,Copy of Petbarn Survey

In Chrome's Developer Tools, if I expand the Window object I can see the node for my data;

gavinData: (7) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]

So why is it undefined at line 445? What do I need to do, to access the contents of the nested array?

Thanks!

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

0

By the nature of promise, you don't know when it resolves(=when the then part run).

First, getDatasources() is executed, then sometime after, which nobody knows, it resolves. Meanwhile these lines below then block

    console.log(window);
    console.log('window.gavinData: Line 445:' + window.gavinData);

are executed. At this point, the promise may have been resolved, may have not.

about 4 years ago · Juan Pablo Isaza Relatório

0

I have managed to work it out. My thinking was all wrong.

I had fallen into the trap of thinking that because I had a .then - and code after the .then call - that it made it automatically available to that later code - which is obviously wrong.

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