Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/suissa/angular-chain-http-promises
Example how chain promises with Angular´s $http
https://github.com/suissa/angular-chain-http-promises
Last synced: 24 days ago
JSON representation
Example how chain promises with Angular´s $http
- Host: GitHub
- URL: https://github.com/suissa/angular-chain-http-promises
- Owner: suissa
- License: mit
- Created: 2016-09-28T03:32:21.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-02T15:37:01.000Z (almost 8 years ago)
- Last Synced: 2024-10-30T01:48:04.487Z (2 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# angular-chain-http-promises
Example how chain promises with Angular´s $http.
```js
angular.module('myApp', [])
.service('CEPService', CEPService)
.controller('MainCtrl', MainCtrl);function MainCtrl ($scope, $http, CEPService) {
/**
Mostrando as 2 formas onde cada resposta é enviada diretamente ao $scope
e usando um Array externo que é populado a cada promise
e na depois de todas ocorrerem uso o finally para jogar o Array p/ o $scope
e pegando os erros com catch
*/const enderecos = []
$scope.error = false
CEPService.find('06608430')
.then((json) => {
$scope.part1 = json.data;
enderecos.push(json.data);
return CEPService.find('80610905');
})
.then((json) => {
$scope.part2 = json.data;
enderecos.push(json.data);
return CEPService.find('80510170');
})
.then((json) => {
$scope.part3 = json.data;
enderecos.push(json.data);
})
.catch(function(err) {
$scope.errorMessage = 'Deu merda: ' + err.toString();
$scope.error = true
})
}
MainCtrl['$inject'] = ['$scope', '$http', 'CEPService']function CEPService ($http) {
const BASE_URL = 'http://labs.edysegura.com/busca-por-cep/cep/endereco.php?cep='
this.find = function (cep) {
return $http.get(BASE_URL + cep)
}
}
CEPService['$inject'] = ['$http']```