Asynchronous Functions in Expressions

Edit in →

Expression questions support custom asynchronous functions that you can use for time-consuming operations, such as requests to a server. In this example, you can select a country from a drop-down list. Two Expression questions use asynchronous functions to load and display the selected country's region and official name.

To implement an asynchronous function for use in expressions, follow the steps below:

  1. Declare an asynchronous JavaScript function.
    This function accepts all arguments in one array-like object. You can pass as many arguments as needed when you call the function from an expression. To return the function's result, pass it to the this.returnResult function.

    function asyncFunc(params) {
        const arg1 = params[0];
        const arg2 = params[1];
        // ...
        setTimeout(() => {
            // Return the function result via the callback
            this.returnResult(yourValue);
        }, 100);
    }
    
  2. Register your function in FunctionFactory.
    Call a static register() method on the FunctionFactory.Instance object. This method accepts a function name that you want to use in expressions, the function itself, and a true flag that indicates an asynchronous function.

    import { FunctionFactory } from "survey-core";
    
    FunctionFactory.Instance.register("asyncFunc", asyncFunc, true);
    
  3. Use your function in an Expression question.
    Specify the question's expression property.

    const surveyJson = {
        "elements": [{
            "type": "expression",
            "name": "asyncExpression",
            // ...
            "expression": "asyncFunc({question1}, {question2})"
        }, {
            // ...
        }]
    };
    
  4. (Optional) Hide an Expression question until it has a value.
    You may want to hide your Expression question until its asynchronous function has executed. To do it, set the visibleIf property to a Boolean expression that uses the notempty function. Refer to the following help topic for more information: Conditional Visibility.

    const surveyJson = {
        "elements": [{
            "type": "expression",
            "name": "asyncExpression",
            // ...
            "expression": "asyncFunc({question1}, {question2})",
            "visibleIf": "{asyncExpression} notempty"
        }, {
            // ...
        }]
    };
    

Your cookie settings

We use cookies on our site to make your browsing experience more convenient and personal. In some cases, they are essential to making the site work properly. By clicking "Accept All", you consent to the use of all cookies in accordance with our Terms of Use & Privacy Statement. However, you may visit "Cookie settings" to provide a controlled consent.

Your renewal subscription expires soon.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.

Your renewal subscription has expired.

Since the license is perpetual, you will still have permanent access to the product versions released within the first 12 month of the original purchase date.

If you wish to continue receiving technical support from our Help Desk specialists and maintain access to the latest product updates, make sure to renew your subscription by clicking the "Renew" button below.