简体   繁体   English

行为与承诺回调混淆

[英]Confusing behavior with Promise Callbacks

Does anyone know how this works? 有谁知道这是如何工作的?

When I declare a Promise in a typescript angularjs class like this: 当我在如下的打字稿angularjs类中声明Promise时:

 constructor() { var promise = customService.getObjects(); promise.then( $scope.successCallback, $scope.errorCallback); $scope.successCallback = () => {} $scope.errorCallback= () => {} } 

...these callbacks won't run when the promise resolves. ...当承诺解决后,这些回调将不会运行。 When I declare them first and then run the promise, then the callbacks work fine. 当我先声明它们然后运行promise时,回调工作正常。 like this: 像这样:

 constructor() { $scope.successCallback = () => {} $scope.errorCallback= () => {} var promise = customService.getObjects(); promise.then( $scope.successCallback, $scope.errorCallback); } 

Is this some tricky javascript/typescript thing? 这是一些棘手的javascript /打字稿吗? It seems quite illogical to me. 对我来说似乎很不合逻辑。

That's because you're just passing 'undefined' into .then() in the first case. 这是因为在第一种情况下,您只是将'undefined'传递到.then()中。 Do the functions need to be attached to $scope? 这些功能是否需要附加到$ scope? Can you just make an anonymous function than are defined inside then()? 您可以只做一个比then()中定义的匿名函数吗? Or you could make these functions themselves call the ones which are attached to $scope and not yet defined. 或者,您可以使这些函数自己调用附加到$ scope且尚未定义的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM