Search code examples
javascriptintellij-ideasuppress-warnings

How to suppress "ignored promise" warnings in IntelliJ, but only for a specific function?


I have a function foo in my project which returns a promise but often it is not needed to wait for the promise to resolve so there is never a .then() handler attached. Intellij IDEA being helpful as it is always marks it with a warning. This can be removed by adding

// JSIgnoredPromiseFromCall

to the top of the file. But this would make it to ignore all ignored promises which is not what we want. Adding the comment for each statement also not ideal since the function is called often so it would be just polluting source code unnecessary.

Is there a way of specifying which exactly function to ignore with file level suppress comment? E.g.

// noinspection JSIgnoredPromiseFromCall[foo]


Solution

  • I doubt you can specify this at the function level other than by not making the function return a promise in the first place, which is how I would solve this. For instance:

    function foo() {
        const promise = /*...foo's original logic...*/;
        promise.catch(error => {
            // ...report the error if that's appropriate...
            // **DO** include this handler even if you don't report it,
            // to avoid unhandled rejection errors
        });
    }
    

    Note that foo doesn't return promise. The promise is handled entirely within foo.

    If there are situations where you do need to use the promise and others where you don't, make two functions and use the appropriate one for the situation:

    function originalFoo() {
        return /*...foo's original logic...*/;
    }
    
    function silentFoo() {
        originalFoo().catch(error => {
            // ...report the error if that's appropriate...
            // **DO** include this handler even if you don't report it,
            // to avoid unhandled rejection errors
        });
    }