Skip to content

Web (HTTP/HTTPS) functions

In browsers, JavaScript provides the non-ECMA-compliant fetch function for HTTP/HTTPS operations. However, fetch is designed for asynchronous environments, where execution continues while waiting for a response. This works well in browsers, but is unsuitable for scripting environments where predictable, sequential execution is required.

To address this, SyncJS provides its own native HTTP/HTTPS client object: HttpCli.

HttpCli operates in a synchro-hybrid way, synchronous within the inividual client session but asynchronous to all other ongoing sessions, ensuring that all web requests are completed before the execution of the individual running script continues. For convenience, it also supports configuration using the "fluent paradigm."

Below are three versions of the same example. The first is traditional, while the second and third use the fluent paradigm.

Example 1: Traditional (non-fluent)

ts
var hc = new HttpCli();
hc.Url("https://www.example.com");
hc.Timeout(30);
hc.Header("Custom-Header", "My custom header content");
var res = hc.Get();

Example 2: Fluent

ts
var hc = new HttpCli();
var res = hc.Url("https://www.example.com").Timeout(30).Get();

Example 3: Fluent and folded

ts
var hc = new HttpCli();
var res = hc.Url("https://www.example.com")
             .Timeout(30)
             .Header("Custom-Header", "My custom header content")
             .Get();