Home How to return AngularJS promise from asynchronous API
Reply: 0

How to return AngularJS promise from asynchronous API

user1758
1#
user1758 Published in June 19, 2018, 2:25 pm

AngularJS: Return values from function

How can I resolve this problem?.

   function byDepartment () {
            var result = null;
            deparmentService.query({
                codDepartment: vm.codDepartment.codDepartment,
                reportBycodDepartment: 1
            }, function (data) {
                var success = JSON.stringify(data.success);
                if (success === false) {
                    console.log(JSON.stringify(data.data));
                    result = data.message;
                } else {
                    result = data.data;
                    console.log('Always has a value: ' + result);
                }
            });
            console.log('Always null: ' + result);
            return result;
        }

The console of the Chrome answer this: Always null: null Always has a value: [object Object],[object Object]

I would like to use this value in the next function after:

  vm.updateChart = function () {
            vm.testValue = 0;
            if (!vm.codTestRoom) {
                console.log('consulta con departamento');
                vm.testValue = byDepartment();
            } else if (!vm.codEquipment) {
                console.log('Consulta con departamento y testRoom');
                vm.testValue = byDepartmentAndTestRoom();
            } else {
                console.log('Consulta con departamento, testRoom y Equipo');
                vm.testValue = byDepartmentAndTestRoomAndEquipment();
            }
            // console.log('Valor: ' + JSON.stringify(vm.testValue));
        };

After this with vm.testValue I'm going to draw a chart with chart.js..

Thanks.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.458285 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO