取消JavaScript承诺

时间:2018-07-02 13:49:37

标签: javascript ecmascript-6 promise es6-promise

我正尝试取消以下承诺:

function example(cancel = Promise.reject()) {
    return new Promise((resolve, reject) => {
        const timer = setTimeout(() => resolve('jack-jack'), 5000);
        cancel.then((res) => {
            clearTimeout(timer);
            reject('cancelled'); 
        }, ()=>{})
    });
}
var cancel=Promise.reject(); 
example(cancel).then((res) => console.log('res handled:' + res)).catch((err) => console.log('err handled:' + err));
console.log('attempting cancellation of promise');
cancel=Promise.resolve();

但是我无法取消它。我在这儿做错什么了?

4 个答案:

答案 0 :(得分:1)

在您的代码中,您已经将完整的(否决的)Promise传递给该函数。并且cancel=Promise.resolve();之后的attempting cancellation of promise不会对传递给example的内容产生任何影响,因为您只是创建了一个新的已解决的Promise。

如果要取消正在运行的进程,则可能需要选择这样的解决方案:

function example(helper) {
  return new Promise((resolve, reject) => {
    helper.cancel = function() {
      clearTimeout(timer)
      reject('cancelled');
    }
    const timer = setTimeout(() => resolve('jack-jack'), 5000);

  });
}
var helper = {};
example(helper).then((res) => console.log('res handled:' + res)).catch((err) => console.log('err handled:' + err));

console.log('attempting cancellation of promise');
helper.cancel()

答案 1 :(得分:0)

因为您正在传递被拒绝的承诺。如果您希望运行cancel.then()块,请传递已解决的承诺。

function example(cancel = Promise.resolve()) {
  return new Promise((resolve, reject) => {
    console.log(cancel);
    const timer = setTimeout(() => {
      resolve('jack-jack'), 5000
    });
    cancel.then((res) => {
      console.log('CANCELLED');
      clearTimeout(timer);
      reject('cancelled');
    }, () => {})
  });
}
var cancel = Promise.resolve();
example(cancel).then((res) => console.log('res handled:' + res)).catch((err) => console.log('err handled:' + err));
console.log('attempting cancellation of promise');
cancel = Promise.resolve();

答案 2 :(得分:0)

由于取消设置为拒绝,则该部分

cancel.then((res) => { //you cannot use then for reject, as reject cannot be resolved.
    clearTimeout(timer);
     reject('cancelled'); 
}, ()=>{})

将永远不会执行,因此您必须解决它,而不是拒绝它

function example(cancel = Promise.reject()) {
    return new Promise((resolve, reject) => {
        const timer = setTimeout(() => resolve('jack-jack'), 5000);
        cancel.then((res) => { //you cannot use then for reject
            clearTimeout(timer);
            reject('cancelled'); 
        }, ()=>{})
    });
}

var cancel = Promise.resolve(); // just change to resolve

example(cancel).then((res) => console.log('res handled:' + res)).catch((err) => console.log('err handled:' + err));

console.log('attempting cancellation of promise');
cancel=Promise.resolve(); // this will have no effect as you have already passed a reject

答案 3 :(得分:-1)

您已将拒绝的承诺分配给变量,将拒绝的承诺传递给函数,并为变量分配了已解决的承诺。该变量采用的两个值是不相关的,您无法更改已兑现承诺的状态。

传递一个您可以解决的诺言

let cancel;
let cancelPromise = new Promise((resolve) => {
    cancel = resolve;
});

example(cancelPromise).then(…).catch(…);
console.log('attempting cancellation of promise');
cancel();

function example(cancel = Promise.reject()) {
    return new Promise((resolve, reject) => {
        const timer = setTimeout(() => resolve('jack-jack'), 5000);
        cancel.then((res) => {
            clearTimeout(timer);
            reject('cancelled'); 
        }, ()=>{})
    });
}

let cancel;
let cancelPromise = new Promise((resolve) => {
    cancel = resolve;
});

example(cancelPromise)
    .then((res) => console.log('res handled:' + res))
    .catch((err) => console.log('err handled:' + err));
console.log('attempting cancellation of promise');
cancel();