0
如何实现承诺的功能,所以我可以摆脱超时,如果它甚至有可能?我正从工厂的一些数据“普里姆”,函数查找的喜欢:函数的承诺,angularjs
$scope.getPre = function(id){
var url = WEB_API.MainUrl + '/api/prim/' + id +'/' + $window.sessionStorage.getItem('idsom');
Prim.getprim(function(data) {
$scope.prim1 = data;
$scope.prim = $scope.prim1[0];
}, url);
$scope.$apply();
}
,我想摆脱掉超时:
setTimeout(function() { // it can't work without timeout
$scope.getPre($routeParams.idprim);
}, 100);
使用回调,它会更清洁 –
只有选项是承诺,有更多的超时项目,我必须改变承诺,如果我使用回调它将是不可读和更复杂 – Mark
var promise = new Promise( (res,rej)=> {foo(/ * callback */res); (')=>/*其中'foo'是异步的东西,你需要等待一个回调,然后在代码的其他地方,你有'setTimeout's而不是'promise.then(()=>/*。 .. * /);' –