Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

completing a promise without reject/resolve?

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?

like image 251
jgauffin Avatar asked Jun 05 '17 08:06

jgauffin


1 Answers

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
    }
});
like image 99
Frederik Hansen Avatar answered Sep 30 '22 17:09

Frederik Hansen