随着jQuery延迟,我以前能够像这样检查当前状态:
var defer = $.Deferred();
defer.state(); //Returns the state of the deferred, eg 'resolved'
Run Code Online (Sandbox Code Playgroud)
Angular deferreds有没有办法做同样的事情?(甚至更好的承诺)
假设有人在一个文件中编写了一个这样的方法,该文件app.js试图通过一个不存在的url来执行XHR请求:
app.controller('MainCtrl', function($scope,$http) {
$scope.send = function() {
$http.get('http://run.plnkr.co/thisIs404');
};
});
Run Code Online (Sandbox Code Playgroud)
我可以http://run.plnkr.co/thisis404在控制台和网络面板中看到有关URL的错误:

要调试这个我想快速找到在源中进行此XHR调用的位置(即查找app.js文件):
所以我启用了chrome dev工具:
调试器实际上在XHR请求上停止,但调用堆栈仅显示对angular.js"核心"文件的引用 :没有对app.js 任何地方的引用.

我尝试使用铬36和铬35.只有解决方案:在整个代码库中搜索错误的URL(在某些情况下可能很难).
app.js堆栈中的somwhere?app.js从控制台错误轻松追踪此文件?使用vanilla XHR请求(即没有角度),XHR调试调用堆栈显示XHR调用app.js(在这种情况下更容易调试):

这里有完整的例子:http://plnkr.co/edit/lnCRpv?p = preview
[编辑]我被问到:Angular.js在我的测试中没有缩小.
javascript ajax asynchronous google-chrome-devtools angularjs
我试图async/await在一个角度1.5.5项目中使用.
鉴于这种服务方法
getDocumentTypes(): angular.IPromise<DocumentType[]> {
var url = "api/document/types";
this.$log.log(url);
return this.$http.get(url).then(_ => _.data);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建async / await该方法的一个版本.
async getDocTypes(): angular.IPromise<DocumentType[]> {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}}
Run Code Online (Sandbox Code Playgroud)
Intellisense显示错误:
TS1055类型'angular.IPromise'不是有效的异步函数返回类型,ES5/ES3因为它不引用与Promise兼容的构造函数值.
有没有使用正确的方式角承诺在打字稿2.1与async/ await?
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 …
是否有最佳实践解决方案能够在承诺中使用?在jQuery中,我可以绑定我的对象以在我的promise/callback中使用它 - 但是在angularJS中?有最佳实践解决方案吗?"var service = this;"的方式 我不喜欢......
app.service('exampleService', ['Restangular', function(Restangular) {
this._myVariable = null;
this.myFunction = function() {
Restangular.one('me').get().then(function(response) {
this._myVariable = true; // undefined
});
}
}];
Run Code Online (Sandbox Code Playgroud)
这个问题有解决方案吗?如何在承诺范围内从我的服务中获取成员或方法?
先感谢您.
当我使用promises来表示作业之间的依赖关系时,解析后的值变得不重要,我可能会忘记某个地方的返回.例:
startSomething().then(function() {
Q.all(tasks.map(function(task) { return task.startIt(); }))
}).then(collectOutput).done();
Run Code Online (Sandbox Code Playgroud)
这里Q.all返回一个承诺,我本应该返回.不这样做意味着collectOutput在调用时,所有任务都已启动,但无法保证它们已完成.
这种错误导致竞争条件,并且可能极难再现和追踪.所以我想知道,是否有一些工具可以帮助检测并避免这种问题?也许有些promise库会在一个函数返回undefined时发出警告?或者检测没有听众的承诺,Bluebird对未处理拒绝的方式呢?
我刚加入了一个用Angular JS制作的项目,它有很多嵌套的承诺.有些任务似乎没有以正确的顺序发生,这导致某些对象在某些时候被覆盖.我需要检查如何/何时调用promises,首先返回,等等.所有这些承诺发生了什么的流程图woudl帮助了我很多,不幸的是我需要花费相当长的时间来手动完成它,所以我想知道是否有一种自动化工具.我一直在谷歌上看,但找不到那样的东西.欢迎任何关于如何跟踪承诺电话/回复的提示!
promise ×6
angularjs ×4
javascript ×4
ajax ×1
async-await ×1
asynchronous ×1
bind ×1
deferred ×1
flowchart ×1
interface ×1
jquery ×1
q ×1
this ×1
tree ×1
typescript ×1