JS承诺未定义的行为

时间:2017-01-09 18:20:30

标签: javascript ecmascript-6 angular-promise es6-promise

问题:需要取消承诺

是否可以取消忽略承诺执行中并在运行时将其替换为另一个承诺,以便只有当前状态解析?

在制作中,我使用角度$timeout来定义cancel()方法。我模仿它在节点中运行:

//Mimic AngularJS's $timeout
function MyTimeout(delay) {
    var t = null;
    var p = new Promise(function (resolve) {
        t = setTimeout(resolve, delay);
    });
    p.realtimeout = t;
    return p;
}
MyTimeout.cancel = function (myto) {
    clearTimeout(myto.realtimeout);
    /*
     * there is no way to appropriately cancel an existing Promise
     * the best I can do in try and change the state of myto to a rejected
     * Promise. Which doesn't help here since it doesn't truly cancel the last
     * Promise...
     */
    myto = Promise.reject("some reason");
};


var x = null;

function changeState() {
    var delay;

    if (x === null) {
        delay = 1000;
    } else {
        MyTimeout.cancel(x);
        delay = 3000;
    }

    x = MyTimeout(delay).then(print_delay);

    function print_delay() {
        console.log("EXECUTED: %s", delay);
    }
}


changeState();

x.then(function () {
    console.log("DONE");
}).catch(console.error);

changeState();

NodeJS输出

(node:12461) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): some reason
(node:12461) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

EXECUTED: 1000
DONE
EXECUTED: 3000

预期结果EXECUTED: 3000应该打印一次。由于设置了标志并且已相应地设置了xx应该指向执行上下文。

1 个答案:

答案 0 :(得分:1)

在使用mypromisex后,您无法将其替换掉,因为更改变量引用不会影响第一个承诺及其处理程序。您需要影响已经拥有的承诺的解决过程,为此您需要以某种方式创建它,以便您可以在调用resolve之后进行更改:

function variableDelay(x) {
    var timer = 0;
    var end;
    return {
        promise: new Promise(resolve => {
            end = () => {
                timer = -1;
                resolve(x);
            };
        }),
        setDelay(d) {
            if (timer < 0) throw new Error("too late");
            if (timer > 0) clearTimeout(timer);
            timer = setTimeout(end, d);
            return this;
        }
    };
}


const {promise:x, setDelay:changeState} = variableDelay();
changeState(1000);
x.then(() => {
    console.log("DONE");
}).catch(console.error);
changeState(3000);