完成承诺而不拒绝/解决? [英] completing a promise without reject/resolve?

查看:53
本文介绍了完成承诺而不拒绝/解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的确认对话框,等待用户输入。我将其包装在一个承诺中。

I got a custom confirm dialog which waits for user input. I'm wrapping it in a promise.

当用户选择是替代项时,我决心

When the user is choosing the "yes" alternative I resolve the promise.

但是,当用户选择 no 时,这并不是一个错误,但更多的是下一个任务不应该被执行。

However, when the user chooses no it's not really an error but more that the next task should not be executed.

我应该如何应对诺言?仅仅是不调用resolve / reject还是有更好的方法?

How should I handle that scenario with the promise? simply not invoke resolve/reject or is there a better approach?

推荐答案

您可以解析一个值,然后再检查该值。 / p>

You could resolve a value and check that value afterwards.

new Promise((resolve, reject) => {
    const userInput = confirm('Do you want to continue?');
    resolve(userInput);
}).then(input => {
    if(input) {
        // Do something
    } else {
        // Do something else
    }
});

这篇关于完成承诺而不拒绝/解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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