Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

use async function in sync function in javascript

(0) ShareShare
ReportReport
Posted on by 60

I have a simple asynchronous function to get the current enviorment variable .I am trying to use this function marked in red in another sync function but getting value as promise pending. I know I am making an error calling this function. Can I get the idea  how to call it and utilize it?

// Asysc to get enviornment variable

GetEnvironmentVariableValue  function (name) {
    let results = await Xrm.WebApi.retrieveMultipleRecords("environmentvariabledefinition", `?$filter=schemaname eq '${name}'&$select=environmentvariabledefinitionid&$expand=environmentvariabledefinition_environmentvariablevalue($select=value)`);

    if (!results || !results.entities || results.entities.length < 1) return null;
    let variable = results.entities[0];
    if (!variable.environmentvariabledefinition_environmentvariablevalue || variable.environmentvariabledefinition_environmentvariablevalue.length < 1) return null;

    return variable.environmentvariabledefinition_environmentvariablevalue[0].value;// returning the value that I want. current value of env varibale
}


//Calling it to another function and getting value as promise pending

UseCurrentValue : function (e) {
        //var formContext= e.getFormContext();  
      
        var alertStrings = { confirmButtonLabel: "Ok", text: "Please wait for it to open an new form.", title: "INFORMATION:" };
        var alertOptions = { height: 300, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
            function success(result) {

                var envURL=  getEnvironmentVariable("new_variable");// Promise Pending
                console.log(envURL);   // promise pending
  ...rest od the code..no need to see..
}}
  • Verified answer
    Ray Profile Picture
    Ray 1,480 on at
    RE: use async function in sync function in javascript

    Actually, your code has used the Promise in this line: Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then

    As the openAlertDialog function also returns a Promise object.

    So just keep going like this:

    getEnvironmentVariable("new_variable").then(
        function (envURL) {
            // do your logic here
        }, function (error) {
            // deal with error
        }
    );

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans