鉴于以下功能:
function isGood(number) {
var defer = $q.defer();
$timeout(function() {
if (<some condition on number>) {
defer.resolve();
} else {
defer.reject();
}
}, 100);
return defer.promise;
}
Run Code Online (Sandbox Code Playgroud)
和一组数字(例如[3, 9, 17, 26, 89]),我想找到第一个 "好"的数字.我希望能够做到这一点:
var arr = [3, 9, 17, 26, 89];
findGoodNumber(arr).then(function(goodNumber) {
console.log('Good number found: ' + goodNumber);
}, function() {
console.log('No good numbers found');
});
Run Code Online (Sandbox Code Playgroud)
这是一个可能的递归版本来实现这个: DEMO
function findGoodNumber(numbers) {
var defer = $q.defer();
if (numbers.length === 0) {
defer.reject();
} else { …Run Code Online (Sandbox Code Playgroud) 我正在尝试多次$http调用,我的代码看起来像这样:
var data = ["data1","data2","data3"..."data10"];
for(var i=0;i<data.length;i++){
$http.get("http://example.com/"+data[i]).success(function(data){
console.log("success");
}).error(function(){
console.log("error");
});
}
Run Code Online (Sandbox Code Playgroud)
我怎么能承诺知道所有的$http电话都是成功的?如果其中任何一个失败,将执行一些操作.
我正在尝试理解promise API和链接,特别$timeout是使用时的时间.then().我对以下内容的期望是,自从$timeout返回一个承诺后,.then()在它解决之前不会被调用.
但是ABAB不是ABAB,而是ABBA.
如何使用promise API确保在执行执行$timeout之前长时间运行的调用(或使用延迟调用)实际上已完成.then()?
码
angular
.module('app', [])
.controller('ThenCtrl', ThenCtrl);
function ThenCtrl($timeout, $q) {
var vm = this;
vm.items = [];
$q.when(pushA()).then(pushB());
$timeout(pushA, 5000).then(pushB());
function pushA() {
vm.items.push('A');
}
function pushB() {
vm.items.push('B');
}
}
Run Code Online (Sandbox Code Playgroud)
标记
<div ng-app="app">
<div ng-controller="ThenCtrl as vm">
{{vm.items}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我设置了一个小提琴:https://jsfiddle.net/kan3c61t/
AngularJS文档说:
$ q promises被临界引擎识别为angular,这意味着在模板中,您可以将附加到范围的promise视为结果值.
那么有人可以解释一下这个小提琴不起作用的原因吗?无法更改文本字段值.但是,分配$ http服务返回到范围字段的承诺就像魅力一样.
控制器:
function MyController($scope, $q, $timeout) {
this.getItem = function () {
var deferred = $q.defer();
deferred.resolve({
title: 'Some title'
});
return deferred.promise;
};
$scope.item = this.getItem();
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<input type="text" ng-model="item.title">
Run Code Online (Sandbox Code Playgroud) 我想将我的模块/ sdk中的promises返回到非角度javascript.例如,如果我返回jquery的承诺,我应该可能发送jquery延迟对象.如何将Angular承诺转换为jquery promise/deferred obj.
任何建议都非常感谢.
一旦promise在angularjs中完成,我就会尝试执行检查.
request.then(function(res){
$ionicLoading.hide();
deferred.resolve(res);
}, function(res){
$ionicLoading.hide();
deferred.reject(res);
})['finally'](function(res){
alert(res)
}
)
Run Code Online (Sandbox Code Playgroud)
但警报将以"未定义"的形式出现.
谢谢
The $q service is very powerful in angularjs and make our life easier with asynchronous code.
I am new to angular but using deferred API is not very new to me. I must say that I completely ok with the How to use part of documentation + there are very useful links for that within the docs + I checked out the source either.
My question is more about the under the hood parts of deferred and promise API objects …
我有一个全局错误处理程序,我的角度应用程序写成$http interceptor,但我想更进一步.我想要的是每次$http失败的电话(被拒绝),承诺的任何"链式"消费者应首先尝试解决错误,如果它仍然未解决(未被捕获),那么我想要全球错误处理程序接管.
用例是,我的全局错误处理程序alert box在屏幕顶部显示一个咆哮.但是我弹出了几个模态,我在那里显式处理错误,在模态本身显示错误信息.因此,基本上,这个模态控制器应该将被拒绝的承诺标记为"已处理".但由于拦截器似乎总是第一个运行$http error,我无法想出办法.
这是我的拦截器代码:
angular.module("globalErrors", ['angular-growl', 'ngAnimate'])
.factory("myHttpInterceptor", ['$q', '$log', '$location', '$rootScope', 'growl', 'growlMessages',
function ($q, $log, $location, $rootScope, growl, growlMessages) {
var numLoading = 0;
return {
request: function (config) {
if (config.showLoader !== false) {
numLoading++;
$rootScope.loading = true;
}
return config || $q.when(config)
},
response: function (response) {
if (response.config.showLoader !== false) {
numLoading--;
$rootScope.loading = numLoading > 0;
}
if(growlMessages.getAllMessages().length) { // clear messages on next …Run Code Online (Sandbox Code Playgroud) - 编辑 -
我最近遇到了一些关于承诺的奇怪的事情,但我想这可能是因为它违背了承诺的哲学.
考虑以下代码:
// Assuming Auth is just a simple lib doing http requests with promises
Auth.signup()
.then(succCall, errCall)
.then(loginSucc, loginErr)
// My callbacks here
function succCall (){
// OK, send second promise
console.log('succCall');
return Auth.login();
}
function errCall(){
// I do some things here and now
// I want to break out from here
console.log('errCall');
}
function loginSucc(){
// This is the callback of the login method when it went OK
// I want to enter here …Run Code Online (Sandbox Code Playgroud) 虽然我正在使用AngularJS中的HTTP promise对象,但我没有一个明确的概念,即HTTP承诺对象实际上是什么,以及AngularJS中HTTP promise对象和传统对象之间的区别是什么!
请问有人解释一下吗?
angular-promise ×10
angularjs ×10
javascript ×5
promise ×5
angular-http ×1
deferred ×1
jquery ×1
q ×1