简体   繁体   English

返回已解决的承诺并在解决后执行代码

[英]return resolved promise and execute the code after resolve

I have the following code which returns a promise, but also should execute some code: 我有以下代码返回一个promise,但还应该执行一些代码:

if (fileExists()) {
     return $q.when().then(function () {
         $scope.$broadcast('create-file-success', file);
     });
}

I sense that this is not the best way to do that, maybe I should use some thing like that: 我觉得这不是最好的方法,也许我应该使用类似的方法:

 return $q.when(function () {
     $scope.$broadcast('create-file-success', file);
 });

or like that: 或类似的:

 return $q(function () {
     $scope.$broadcast('create-file-success', file);
 });

But I can't figure out from the documentation whether the last two do the same. 但是我无法从文档中找出最后两个是否相同。

It should be something like following : 它应该类似于以下内容:

 $q.when(fileExists()).then(function (data) {
     console.log('Resolved with the value data', data);
     //do whatever you want to do with the resolved object and data.
     $scope.$broadcast('create-file-success', file);
 });

I hope this helps :) 我希望这有帮助 :)

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

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