Wikis - Page

Asynchronous JavaScript APIs in TruClient – Part III

0 Likes

Asynchronous JavaScript APIs In TruClient – Part I

Asynchronous JavaScript APIs In TruClient – Part II

Advanced Examples

Asynchronous API call chains

It is common to execute multiple asynchronous operations one at a time, where each operation starts when the previous operation successfully finishes, with the result from the previous step. This can be accomplished using a promise chain.

Below is a synchronous code example to check if a file exists before reading it:

var fileToRead = "C:\\Demo\\1.txt";

 

if (IO.isExist(fileToRead))

{

  var content = IO.read(fileToRead);

  TC.outputMessage(content);

}

Example: Using a promise chain

The example below shows a promise chain using asynchronous code. Note that the callback function of the method of the promise object returned by IOA.isExist uses the statement “return IOA.read(fileToRead);” to return the promise object returned by IOA.read.

var fileToRead = "C:\\Demo\\1.txt";

 

IOA.isExist(fileToRead).then(function(isExist) {

  if (isExist) {

    return IOA.read(fileToRead);

  } else {

    TCA.outputMessage(fileToRead " doesn't exist");

  }

}).then(function(fileContent) {

  if (fileContent) {

    TCA.outputMessage(fileContent);

  }

 

  TCA.done();

}).catch(function(error) {

  TCA.doneWithError(error);

});

Example: Using async/await

The example below shows asynchronous code using async/await:

((async () => {

  let fileToRead = "C:\\Demo\\1.txt";

  let isExist = await IOA.isExist(fileToRead);

  if (isExist) {

    let content = await IOA.read(fileToRead);

 

    await TCA.outputMessage(content);

  } else {

    await TCA.outputMessage(fileToRead " doesn't exist");

  }

})()).then(

  (result) => { TCA.done(result); }

).catch(

  (error) => { TCA.doneWithError(error); }

);

Asynchronous operations in parallel

One of the advantages of asynchronous operations is they can be run in parallel, whereas synchronous operation can only be read one at a time.  

Let’s take the scenario of reading content from three files. Suppose it takes one second to read file 1, two seconds to read file 2, and three seconds to read file 3. Reading them one at a time using synchronous code would take six seconds, while reading them in parallel using asynchronous code (either Promise.all or async/await) would take only three seconds.

Example: Using Promise.all to run operations in parallel

Here is an example of running asynchronous operations in parallel using Promise.all. It can be used in all TruClient browsers.

var promise1 = IOA.read("C:\\Demo\\1.txt");

var promise2 = IOA.read("C:\\Demo\\2.txt");

var promise3 = IOA.read("C:\\Demo\\3.txt");

 

Promise.all([promise1, promise2, promise3]).then(function(result){

  // result is an array contains content of all three files

  TC.outputMessage(result[0]);

  TC.outputMessage(result[1]);

  TC.outputMessage(result[2]);

 

  TCA.done(result);

}).catch(function(error) {

  TCA.doneWithError(error);

});

 

Example: Using async/await to run operations in parallel

Here is an example of running asynchronous operations in parallel using async/await:

((async () => {

  let content1 = await IOA.read("C:\\Demo\\1.txt");

  let content2 = await IOA.read("C:\\Demo\\2.txt");

  let content3 = await IOA.read("C:\\Demo\\3.txt");

 

  await TCA.outputMessage(content1);

  await TCA.outputMessage(content2);

  await TCA.outputMessage(content3);

})()).then(

  (result) => { TCA.done(result); }

).catch(

  (error) => { TCA.doneWithError(error); }

);

Should I use Promise.all or async/await?

When calling asynchronous APIs in parallel, I would recommend using Promise.all because I found it better at exception handling, and, async/await can work with TruClient Chromium and TruClient Browser only. However, you can read the various discussions about these methods and reach your own conclusions.

Conclusion

This is the last post in the TruClient asynchronous APIs series. I hope you found it useful. You are welcome to let us know what you think by leaving a comment in the box below.

For the latest information on using TruClient asynchronous APIs, see the TruClient Help Center.

Labels:

How To-Best Practice
Education-Training
Comment List
Related
Recommended