本文介绍了如果没有其他函数链接到promise,则为默认行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开'确认你要取消对话',如果没有其他功能被链接,则默认导航回页面( $ window.history.back(); )。

I want to open a 'confirm you want to cancel dialog', and if no other functions are chained up, then default to navigating back a page ($window.history.back();).

如果我传入回调,我就是这样做的:

This is how I might do it if I was passing in a callback:

function openCancelModal(form, callback) {
     if (form.$dirty) {
         var message = '...';
         modalThingy.open(message).then(callback || default);
     }

     function default() {
         $window.history.back();
     }
}

然而这吞噬了使用承诺的力量(凉爽) 。

However that swallows the power (coolness) of using promises.

有没有办法返回一个承诺,但是如果没有任何东西被链接到它然后调用一个默认函数?

Is there a way of returning a promise, but if nothing is chained to it then call a default function?

我使用的是angularjs,但我想这个问题有一个通用的答案。

I am using angularjs, but I imagine there is a generic answer to this problem.

推荐答案

当你从函数返回promise时,调用者还没有看到promise对象,所以它甚至没有机会看到调用者将会是什么在承诺被退回之后不会做,或者在您退回承诺之前无法预先设置或看到任何内容。

At the point you are returning the promise from the function, the caller has not even seen the promise object yet so it has had no opportunity to even see what the caller will or won't do after the promise is returned so nothing can be set up or seen in advance before you return the promise.

此外,标准承诺对象不提供任何查询处理程序是否附加的方法。

In addition, standard promise objects do not provide any means of querying what handlers are or are not attached.

有一些可以做的hackish事情(虽然我不认为我会推荐它们)。

There are some hackish things that could be done (though I don't think I'd recommend them).

例如,您可以覆盖 .then()关于您要返回的承诺,以便您能够在承诺解决之前查看它是否被调用,您可以在自己的状态中跟踪该信息。然后,当promise得到解决时,如果尚未调用 .then(),则可以执行默认行为。

For example, you could override .then() on the promise you are returning so that you will be able to see if it gets called before the promise is resolved and you could keep track of that info in your own state. Then, when the promise does get resolved, if .then() has not yet been called, you could execute your default behavior.

以下是这种hackish方案的概述:

Here's an outline of that sort of hackish scheme:

function someAsync() {
    var foundThenHandler = false;
    var p = new Promise(function(resolve, reject) {
        // some code that eventually and asynchronously
        // calls resolve or reject


        // as long as the code here executes some time in the future
        // after the caller has seen the returned promise and had a chance to 
        // add its own .then() handlers, then the code can check the
        // foundThenHandler variable to see if .then() was ever called on it

        // For example:
        setTimeout(function() {
            resolve();
            if (!foundThenHandler) {
               // carry out some default action
            }
        }, 1000);
    });
    var oldThen = p.then;
    p.then = function(resolveHandler, rejectHandler) {
        foundThenHandler = true;
        return oldThen.apply(p, arguments);
    }
    return p;
}


someAsync().then(function(val) {
    // async operation finished, default action will not be triggered
});

// no .then() handler attached, default action will be triggered
someAsync();

这篇关于如果没有其他函数链接到promise,则为默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 23:25