简体   繁体   English

在另一个承诺链中解决承诺并停止该链

[英]Resolve promise in another promise's chain and stop that chain

I want to do something simple: I want my function to return a promise(1). 我想做一些简单的事情:我希望我的函数返回一个promise(1)。 This promise(1) will use another promise(2) to perform a task. 此promise(1)将使用另一个promise(2)执行任务。 Promise(2) is chained to multiple then/catch steps. Promise(2)链接到多个then / catch步骤。 Inside those steps, promise(1) may need to be resolved/rejected. 在这些步骤中,可能需要解决/拒绝promise(1)。 The chain of promise(2) needs to stop executing as now promise(1) is resolved/rejected and should stop running. 由于现在已经解决/拒绝了promise(1),promise(2)链需要停止执行,并且应该停止运行。

What's the best way to do this? 最好的方法是什么?

See the following example code: 请参见以下示例代码:

function performDiv(a, b) {
    return new Promise(function (res, rej) { // promise(2)
        if (b === 0) {
            return rej(new Error('div by 0'));
        }
        res(a / b);
    })
}

function div(a, b) {
    return new Promise(function (res, rej) { // promise(1)
        performDiv(a, b)
        .then(res) // <--- HERE I want to break the chain
        .catch(rej) // <--- OR HERE, if there's a problem
        .then(function () {
            console.log('I don\'t want this to be shown')
        });
    });
}

div(10, 2)
.then(function (result) {
    console.log(result);
})
.catch(function (err) {
    console.log(err);
});

Currently, the I don't want this to be shown (a hypothetical next step in order to resolve the promise(1)) is shown while I want to find a solution where it isn't, as the promise(1) is already resolved/rejected. 当前, I don't want this to be shown (为了解决promise(1)的假设性下一步),而我想查找不存在的解决方案,因为promise(1)已经存在解决/拒绝。

I think the problem here is misunderstanding of how a promise work. 我认为这里的问题是对诺言如何运作的误解。

Only way to break the promise chain is to throw an error and not catch it as until end. 打破承诺链的唯一方法是抛出一个错误,直到捕获错误为止。

Let's take your example. 让我们举个例子。

function div(a, b) {
    return new Promise(function (res, rej) { // promise(1)
        performDiv(a, b)
        .then(res) // <--- HERE I want to break the chain (A)
        .catch(rej) // <--- OR HERE, if there's a problem (B)
        .then(function () { // (C)
            console.log('I don\'t want this to be shown')
        });
    });
}

Both A & B could throw an error to break the chain of then -ables. A和B都可能抛出错误以破坏then -ables链。

If (A) throws an error B will catch it. 如果(A)抛出错误,B将捕获该错误。 In B you are calling reject function, which sets Promise 1 as rejected, however it continues to execute remaining chain on Promise 2, because, rej method returns a value (it could be undefined), which makes that promise (the one catch returns) to resolve. 在B中,您正在调用拒绝函数,该函数将Promise 1设置为被拒绝,但是它继续执行Promise 2上的剩余链,因为rej方法返回一个值(它可能是未定义的),从而产生了诺言(一个catch返回)解决。

Take away: each then or catch themselves returns a Promise. 带走: then每个或catch自己返回一个承诺。 To break the chain throw an error and do not catch it until the end. 要打断链条,请抛出错误,直到结束时才捕捉。

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

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