P4VJS Documentation | 2025.1

# Call P4VJS functions

Every P4VJS asynchronous method (P4VJS.<method>) returns a JavaScript Promise instread of a value. This means that you need to provide a promise consumer, which is a callback method that gets called when the request is processed. This section explains how to handle execution of the promise as:

- A callback method
- An inline unnamed callback method

If the application requires synchronous behavior, you can use the keyword await, which makes JavaScript wait until the promise settles. You cannot use await in a regular synchronous function. To use await, you have to make the function that calls it async. If a function returns a value, changing it to async affects the function's return value. As an asynchronous function, it now returns a promise hosting the return value.

You cannot use await in top-level functions, but you can create an asynchronous Immediately InvokedFunction Expression.

    (async function(){
        ... asynchronous code goes here
        let value = await asyncFunction(...);
    }())

For more information on JavaScript promises, see https://promisesaplus.com.

Following are implementation examples:

- Asynchronous named

  Only p4vjs.p4(..) supports a callback argument (this is the function to be called, with the return value as the parameter).

      p4vjs.loadServerInfoCallBack(serverInfo){
      ….
      }

      var loadcallback = loadServerInfoCallBack;
      p4vjs.p4(‘info’,’’,loadcallback);

- Asynchronous anonymous

      p4vjs.functionName(arguments).then(function(retvalue) {
      ….
      ….
      });

- Synchronous

      async function outerfunction() {
            var myVar = await p4vjs.functionName(arguments);
      }

  function outerfunction() now returns a promise. If handling the return value is required, it needs to be handled as an asynchronous function.

Your search for returned results