Necesito hacer varias llamadas a una API y concatenar los resultados. estoy usando angularjs
$scope.myFunction = function() { let res; for (//some condition) { let data = MyService.query(); data.$promise.then(function(r){ // here I receive the response from the api and I "concatenate" the result in a variable res += r.someValue; } } // here I need to do something with **res** when all the api calls are done }el problema es que el código fuera del bucle for se ejecuta antes de que todas las llamadas a la API devuelvan un resultado.
¿Cómo puedo hacer que el código después del ciclo for espere a que se ejecute todo el código dentro del ciclo y la variable res esté completa? Usar async/await no funciona porque recibo este error
"angular.js:12808 ReferenceError: regeneratorRuntime is not defined"y por el momento no puedo agregar paquetes a package.json.
Puedes usar Promise.all algo como
$scope.myFunction = function() { let res=[]; for (//some condition) { let data = MyService.query(); let respPromise= data.$promise.then(function(r){ // return the response return r.someValue; }) res.push(respPromise); } Promise.all(res).then(data=>{ console.log(data);//data should contains all the responses here }) }apilado de trabajo