Async function in Expression


//Hashtable for countries information
var countriesInformation = {};
//The list of listeners
var isReadyCallbackList = [];
function setValueAndnotifyListeners(country, res) {
    countriesInformation[country] = res;
    for(var i = 0; i < isReadyCallbackList.length; i ++) {
    isReadyCallbackList = [];
function getCountryInfoAjax(survey, country, isReadyCallback) {
    if (!country) {
        isReadyCallback({ isFound: false });
        return null;
    var countryInfo = countriesInformation[country];
    if(!!countryInfo && (countryInfo.status == "processing")) {
        return null;
    if(!!countryInfo  && countryInfo.status == "completed") {
        return countryInfo;
    countriesInformation[country] = {status: "processing"};

    survey.setVariable("country_request_processing", true);
        url: "" + country,
        type: "GET",
        success: function (data) {
            survey.setVariable("country_request_processing", false);
            if (!data || data.length < 1) {
                setValueAndnotifyListeners(country, {status: "completed", isFound: false});
            var countryValue = data[0];
            setValueAndnotifyListeners(country, {status: "completed", isFound: true, officialName: countryValue.officialName, region: countryValue.region});
        error: function (xhr, ajaxOptions, thrownError) {
            survey.setVariable("country_request_processing", false);
            setValueAndnotifyListeners(country, {status: "completed", isFound: false});
    return null;
function getCountryInfo(context, params, property) {
    if(params.length < 1) {
    var self = this;
    var isReady = function(countryInfo) {
    return getCountryInfoAjax(context.survey, params[0], isReady);
function getCountryOfficialName(params) {
    return getCountryInfo(this, params, "officialName");
function getCountryRegion(params) {
    return getCountryInfo(this, params, "region");
function isCountryExist(params) {
    return getCountryInfo(this, params, "isFound");

Survey.FunctionFactory.Instance.register("getCountryOfficialName", getCountryOfficialName, true);
Survey.FunctionFactory.Instance.register("getCountryRegion", getCountryRegion, true);
Survey.FunctionFactory.Instance.register("isCountryExist", isCountryExist, true);

                    var json = {
  checkErrorsMode: "onValueChanged",
  elements: [
      type: "html",
      name: "requesting",
      html: "The data is requesting",
      visibleIf: "{country_request_processing} = true"
      type: "text",
      name: "country",
      title: "Please enter the country name:",
      "placeHolder": "Estonia",
      validators: [
          type: "expression",
          expression: "isCountryExist({country}) = true",
          text: "We could not find country with this name."
      "type": "expression",
      "name": "officialName",
      "title": "Oficial name {country} is:",
      "expression": "getCountryOfficialName({country})",
      "visibleIf": "{officialName} notempty"
      type: "expression",
      name: "region",
      title: "{country} is located in region:",
      expression: "getCountryRegion({country})",
      visibleIf: "{region} notempty"

                    window.survey = new Survey.Model(json);
            survey.onComplete.add(function(sender) {
            document.querySelector('#surveyResult').textContent =
            "Result JSON:\n" + JSON.stringify(, null, 3);
                ko.applyBindings({ model: survey }, document.getElementById("surveyElement"));

<!DOCTYPE html>
<html lang="en">
    <title>Async function in Expression, Knockoutjs Survey Library Example</title>
<meta name="viewport" content="width=device-width" />
    <script src=""></script>
    <script src=""></script>
    <script src="/DevBuilds/survey-core/survey.core.min.js"></script>
    <script src="/DevBuilds/survey-core/survey.i18n.min.js"></script>
    <script src="/DevBuilds/survey-knockout-ui/survey-knockout-ui.min.js"></script>
    <link href="/DevBuilds/survey-core/defaultV2.min.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="./index.css">

<body style="margin: 0">
            <div id="surveyElement" style="display:inline-block;width:100%;">
                    <survey params="survey: model"></survey>
    <div id="surveyResult"></div>

<script type="text/javascript" src="./index.js"></script>

Sorry, we can't retrieve the data from server. Please comeback later.

Why we use cookies.

This site uses cookies to make your browsing experience more convenient and personal. Cookies store useful information on your computer to help us improve the efficiency and relevance of our site for you. In some cases, they are essential to making the site work properly. By accessing this site, you consent to the use of cookies.

For more information, refer to DevSoft Baltic’ privacy policy and cookie policy.