承诺取消仍然触发履行功能 [英] promise cancellation still firing fulfill function

查看:53
本文介绍了承诺取消仍然触发履行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

var promiseResult =  new BBpromise(function(resolve, reject){
    console.log(1)
    // some actions
    setTimeout(function(){
        resolve();
    }, 2000);
}).cancellable().catch(BBpromise.CancellationError, function(e){
    console.log(e);
});

promiseResult.then(function(){
    console.log('AYYYY');
});
promiseResult.cancel(new BBpromise.CancellationError()).then(function(s){
    console.log('canceled ?');
});

我得到以下输出:

顺便说一句..输出似乎是立即的.好像只是跳下决心.

BTW.. the output seems to be immediate. Seems like if just jump that resolve.

1
{ [CancellationError: cancellation error] __stackCleaned__: true }
AYYYY
cancelled ?

它说.."stactCleaned:true",所以我想这意味着所有的执行功能都被清除了.但似乎我错了

It says.. "stactCleaned: true" so I guess that means that all the fulfil functions were cleaned. But seems I'm wrong

所以我的问题:

  • 为什么当我调用cancel()时,如果诺言被取消,在输出中我会看到'AYYYY'.
  • 这里的取消实际上是做什么的?在这种情况下,取消实际上是做什么的?
  • 是否有一种方法可以取消执行承诺的内容或取消承诺的传播?
  • why when I call to cancel(), in my output I see 'AYYYY' if the promise was cancelled.
  • What's exactly doing cancel in here? what is cancel actually doing in this case?
  • Is there a way to cancel the execution of the content of a promise or the propagation of the promise?

推荐答案

为什么当我调用cancel()时,如果诺言被取消,在输出中我会看到"AYYYY".

why when I call to cancel(), in my output I see 'AYYYY' if the promise was cancelled.

这是.catch()的通常行为.

使用catch()时,您正在捕获取消错误,这会产生一个新的Promise,并使用值undefined进行了解析(因为您没有从.catch()处理程序返回任何内容).

With catch(), you are catching the cancellation error and this produces a new promise that is resolved with the value undefined (because you're not returning anything from the .catch() handler).

所以您提供给then的函数就可以了.

So the function you provide to then is called just fine.

在这种情况下取​​消实际上是做什么的?

What is cancel actually doing in this case?

来自Bluebird文档:

From the Bluebird documentation:

取消承诺会传播到仍待处理的目标承诺的最远可取消祖先,并拒绝给定原因的给定原因(默认情况下为CancellationError).

它正在遍历承诺链,并拒绝您创建的第一个承诺(并标记为.cancellable()). .cancel()最多拒绝单个承诺,而不是整个链.

It is travelling up the promise chain and rejecting that first promise that you created (and which you marked as .cancellable()). .cancel() rejects at most a single promise, not an entire chain.

是否可以取消执行承诺内容或承诺的传播?

Is there a way to cancel the execution of the content of a promise or the propagation of the promise?

您不能取消已在运行的代码的执行,对于要传递给Promise构造函数的函数中的代码,情况就是如此.

You can't cancel the execution of code that is already running, which is the case for the code in the function that you're passing to the Promise constructor.

如果分辨率尚未传播到承诺链的下方,您可以 取消处理程序的执行.

You can cancel the execution of handlers further down the promise chain if the resolution hasn't propagated there yet.

您可以取消承诺的传播.您正是在这样做.但是,如果您catch取消,然后将任何内容链接到该catch,您将重新开始承诺链.

You can cancel the propagation of a promise. You are doing precisely that. But if you catch the cancellation and then chain anything onto that catch, you begin the promise chain anew.

也许这会有所启发:

var promiseResult =  new Promise(function(resolve, reject){
    console.log(1)
    setTimeout(function(){
        resolve("1");
    }, 2000);
}).cancellable().then(function () {
    console.log("this is never called");
    return 2;
}).catch(Promise.CancellationError, function(e){
    console.log(e);
    return 3;
});

promiseResult.then(function(d){
    console.log("got the value " + d);
    return 4;
});
promiseResult.cancel(new Promise.CancellationError()).then(function(s){
    console.log("and I got the value " + s);
    return 5;
});

输出为:

1
CancellationError: ...
got the value 3
and I got the value 3

如您所见,原始的诺言被取消,因此从未被调用"部分从未被调用.您有catchthen调用可将链进一步向下恢复.

As you can see, the original promise is cancelled, so the "this is never called" part is never called. You have catch and then calls that resume the chain further down.

这篇关于承诺取消仍然触发履行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆