简体   繁体   English

使用Angular $ q进行解析调用时,如何获得Promise链中最后一个then函数的结果?

[英]How to get result of the last then function in chain of promises when making resolve call with Angular $q?

The problem I am experiencing is a bit hard to explain, and I might getting (Angular) promises wrong, but still... 我遇到的问题很难解释,我可能会(Angular)答应错,但仍然...

I am trying to handle the following situation nicely. 我正在努力很好地处理以下情况。 In general, let's say I want to have my Angular dialogService to provide a confirm method, which would return a promise resolved on clicking yes button, it means when the confirm actually succeeded. 在一般情况下,比方说我希望有我的角度dialogService提供confirm方法,该方法将返回解决上点击一个承诺yes按钮,这意味着当确认实际成功。 However, I want the dialog would stay open until the internal async operation--which would be executed on yes confirmation--finishes. 但是,我希望对话框可以保持打开状态,直到内部async操作(将在yes确认后执行)结束为止。 And if it's finishes successfully, then the dialog should close, otherwise stay open. 如果成功完成,则对话框应关闭,否则保持打开状态。

In code that would be (perfectly) looking like this: 在(完全)看起来像这样的代码中:

the outer code: 外部代码:

    dialogService.confirm('title', 'message').then =>
        return myLongLastingOperationReturningPromise()

the confirm method implementation something like this: confirm方法的实现是这样的:

    def = $q.defer()

    dialog = ngDialog.open(...)
    // closePromise or any other custom local promise
    dialog.closePromise.then =>
        // this is fake, but how can I achieve this?
        result = def.resolve('closeRequest');
        if(typeof result.then == 'function') {
            result.then =>
                // continue closing the dialog
        } else if (result === false) {
            // just do nothing
        } else {
            // closing the dialog
        }

in other words, is there any way to obtain the result of the last then method in the promises chain on/after calling resolve ? 也就是说,有没有什么办法来获得最后的结果, then调用后在/承诺链方法resolve

You should execute the confirm after the API has successfully returned. API成功返回后,您应该执行确认。

eg Click 'yes' button will execute method YesHandler: 例如,单击“是”按钮将执行方法YesHandler:

//$scope is the ngDialog scope here
$scope.YesHandler = function () {
  myLongLastingOperationReturningPromise().then(function(data) {
    //Execute confirm method after API returned
    $scope.confirm(data);
  })
}

In caller: 在呼叫者中:

modalInstance = ngDialog.openConfirm({
                    template: 'xxx.tpl.html',
                    scope: $scope,
                    controller: 'xxxCtrl'
                    });

modalInstance.then(function (data) {
  //This data is returned from confirm
});

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

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