jgauffin
jgauffin

Reputation: 101130

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?

Upvotes: 6

Views: 5720

Answers (1)

Frederik Hansen
Frederik Hansen

Reputation: 506

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

Upvotes: 10

Related Questions