How to get return value from httpClient

Hi guys,
I’ve been using ACE and I got the issue information I wanted but I can’t get the return value of the function. Can you help me out here?

httpClient.get('https://xxx.atlassian.net/rest/api/2/issue/TA-3?fields=summary,description', 
        function (err, res, body) {
          const issueResponse = JSON.parse(body);
          const summary = issueResponse.fields.summary;
          const description = issueResponse.fields.description;
      });

I wanna return the values description and summary so I can use them outside the function but I dunno how.

Hi Peter,

I think you are running into the standard asynchronous javascript challenge. The result values are only available in the callback function, which will fire once httpClient gets the response. In the callback approach, have to do further processing within the callback function itself (not the main line).

For our addon we promisified the httpClient so we could use it in async/await style. Either can work. See Async/await

2 Likes

Hi @PeterStben consider to do it like the following:

inside app.js we exported the ACE addon

...
export { app, addon };

Then we are importing it to an utils module and Promisfy the GET request to Jira API where req is the general Express.js request object:

import { addon } from "../app";

export const get = ( req, endpoint ) => {
  // Return promise
  return new Promise( ( resolve, reject ) => {
    // Call Jira PI
    addon.httpClient( req ).get( endpoint, ( err, response, body ) => {

      // Reject with error
      if (err) 
        reject(err);

      // Resolve with data
      resolve( JSON.parse(body) );
    });
  }) 
};

Finally you can get the return value in any (controller) function:

import { get } from "../utils/utils"

export const anyFunction = async ( req, res) => {
    // get the jira components
    const jDump = await get(req, `/rest/api/3/project/${req.body.projectKey}/component`);
    ...
}

Best Valentin

2 Likes