completing a promise without reject/resolve?

一笑奈何 提交于 2019-12-23 20:18:51

问题


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.

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

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


回答1:


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
    }
});


来源:https://stackoverflow.com/questions/44364899/completing-a-promise-without-reject-resolve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!