Automations
All questions related to Workflow Automation, AutomationEngine, and EdgeConnect, as well as integrations with various tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Keep Getting Promise { <Pending> } When Returning Current Version of Extension and Latest Version in Hub

badgerfifteen
Organizer

Hi all,

I'm currently working on a Code dashboard tile which shows the current version of an extension in relation to the latest version posted in the hub. I'm hoping to use this code for Workflows so I can be notified when extensions fall out of date. But I need to get over this first hurdle. 

This is the code I have created so far:

async function checkVersion(extensionName) {
  // fetching extensions from the hub
  const response = await fetch(
    'https://{environmentid}.live.dynatrace.com/api/v2/hub/extensions2/' + extensionName,
    {
      headers: {
        method: "GET",
        Accept: "application/json; charset=utf-8",
        'Authorization': 'Api-Token <REDACTED>'
      }
    }
  );
  const result = await response.json();
  return result.extension2Details.recommendedCatalogVersion;
}

export default async function () {

  // fetching current extensions in environment
  const response = await fetch(
    'https://{environmentid}.live.dynatrace.com/api/v2/extensions',
    {
      headers: {
        method: "GET",
        Accept: "application/json",
        'Authorization': 'Api-Token <REDACTED>'
      }
    }
  );
  const result = await response.json();

  const itemsArray = Object.values(result.extensions);

  const filteredItems = itemsArray.map(extension => ({
    'Extension Name': extension.extensionName,
    'Version': extension.version,
    'Version To Date': checkVersion(extension.extensionName).then()
  }));

  //console.log(checkVersion("com.dynatrace.filesystem").then());
  return filteredItems;
}

 I realise this may be an issue with items not being resolved yet as part of the checkVersion() function but even manually calling it makes this appear in the console:

Promise { <pending> }

..which I assume is happening in the table generated from this, too.

If I use await like this:

await checkVersion("com.dynatrace.filesystem")

…I do get some data back, but I can't use it in the mapping function as I get this:

"message": "Parse error: await isn't allowed in non-async function at file:///script.ts:34:30\n\n 'Version To Date': await checkVersion(extension.extensionName)

 

1 REPLY 1

Evan1
Frequent Guest

Hi badgerfifteen,

As the error message indicates, you must be in an asynchronous function to use the "await" prefix. This prefix is ​​necessary to wait for the promise to resolve before proceeding to the next step. Currently, your function is asynchronous, but the mapping is not:

const filteredItems = itemsArray.map(extension => ({ // No async prefix here
    'Extension Name': extension.extensionName,
    'Version': extension.version,
    'Version To Date': checkVersion(extension.extensionName).then() // Need to be proceed before  
}));

You need to have something like that : 

itemsArray.map(async (extension) => ({
  ...
})

 

To fix this, you can do : 

async function checkVersion(extensionName) {
  // fetching extensions from the hub
  const response = await fetch(
    'https://{environmentid}.live.dynatrace.com/api/v2/hub/extensions2/' + extensionName,
    {
      headers: {
        method: "GET",
        Accept: "application/json; charset=utf-8",
        'Authorization': 'Api-Token <REDACTED>'
      }
    }
  );
  const result = await response.json();
  return result.extension2Details.recommendedCatalogVersion;
}

export default async function () {
  // fetching current extensions in environment
  const response = await fetch(
    'https://{environmentid}.live.dynatrace.com/api/v2/extensions',
    {
      headers: {
        method: "GET",
        Accept: "application/json",
        'Authorization': 'Api-Token <REDACTED>'
      }
    }
  );
  const result = await response.json();

  const itemsArray = Object.values(result.extensions);

  // Use Promise.all to handle asynchronous operations
  const filteredItems = await Promise.all(itemsArray.map(async (extension) => {
    const versionToDate = await checkVersion(extension.extensionName);
    return {
      'Extension Name': extension.extensionName,
      'Version': extension.version,
      'Version To Date': versionToDate
    };
  }));

  return filteredItems;
}

 

Let me know if it fix your problem

Have a nice day,

 

Sincerely

Featured Posts