Search code examples
javascriptes6-promise

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?


Solution

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