0

I'm using Promise.reject

I've got this warning: Unhandled promise rejection warning: version is not released

how can I solve this warning ? I'm trying to use try and catch

Thanks for your help

 public async retrieveVersionFromJira(versionName: string): 
 Promise<ReleaseVersion> {
    const searchVersionsUri = config.jiraApiUri + 'versions';
    const jsonResp = await this.jiraClient.get(searchVersionsUri);
    const version: any = jsonResp.find(version => {
        if (version.name == versionName) {
            if (version.released == true) {
                try{
                  return Promise.reject("version " + versionName + " is not released");
               }
               catch{
                 return Promise.reject("error test")
               }
            }
        }
    });
    if (!version) {
        return Promise.reject("missing version " + versionName + " on jira");
    }
    return new ReleaseVersion(version.id, version.name, version.released);
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
jeremy
  • 165
  • 10

1 Answers1

1

There are probably two problems:

  1. Not handling rejection. This problem isn't in retrieveVersionFromJira, it's in the code using it. That code needs to handle the fact that it may reject its promise. Apparently, the code using it is only handling success, not failure.

    One of the basic rules of promises (and thus, async functions, which return promises) is that you must either handle rejection (errors), or pass the promise on to a calling function that will handle rejection.

    If you're calling it from an async function, that function will automatically pass on rejection to its caller (which should either pass it on or handle it). If you're using a top-level async function, it needs to never reject (by using try/catch to catch all errors/rejections that occur within it), like this:

    // If this is the top level
    (async function() {
        try {
            const version = retrieveVersionFromJira("name");
            // ...use `version`...
        } catch {
            // Handle/report error
        }
    })();
    

    If you're calling it from a non-async function, that function also must either return the promise chain to its caller (which should either pass it on or handle rejection) or handler rejection, e.g.:

    // Pass it on
    function someFunction() {
        return retrieveVersionFromJira("name")
            .then(version => {
                // ...use the result...
            });
    }
    
    // Or handle rejection
    function someFunction() {
        retrieveVersionFromJira("name")
        .then(result => {
            // ...use the result...
        })
        .catch(error => {
            // Handle/report the error
        });
    }
    
  2. The code in retrieveVersionFromJira calling jsonResp.find is incorrect. You've said that jsonResp is an array¹. Array.prototype.find expects the callback to return a truthy or falsy value indicating whether the current entry is the one you want to find. Your code attempts to return from retrieveVersionFromJira from within the callback, which it can't do. You also have if (version.released == true) followed by return Promise.reject("version " + versionName + " is not released");, which doesn't seem to make sense. You probably wanted:

    const version: any = jsonResp.find(version => version.name === versionName);
    if (version && !version.released) {
        return Promise.reject("version " + versionName + " is not released");
    }
    

    ...but see the note under the line below about return Promise.reject(...) not being the best way to handle rejecting the promise from an async function.

    ¹ ...in which case, it isn't JSON. JSON is a textual notation for data exchange. If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON.


Side note: Although it isn't the problem, the code in retrieveVersionFromJira shouldn't be using Promise.reject. See this question's answers, the way to reject the promise from an async function is to use throw. Everywhere you've used return Promise.reject(x); you should be using throw x;. Also, since rejections are errors, it's usually best to use Error instance (e.g., throw new Error("missing version " + versionName + " on jira");.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • @jeremy - No, you wouldn't do it like that. Again: The problem isn't in that function, it's in the code that **calls** that function. – T.J. Crowder Aug 29 '19 at 11:39
  • @jeremy - Separately: The way SO works, questions are not meant to be moving targets, you don't edit the question to try to fold in answers. I've rolled back that edit. – T.J. Crowder Aug 29 '19 at 11:42
  • Ok so it's not necessary to change this function, i'm trying – jeremy Aug 29 '19 at 11:49