The promise()
function returns callable object(function) that has method .then(onFulfilled, onRejected)
The promise is created either as (case #1):
var prom = promise();
or as (case #2)
var prom = promise( function( resolve:function, reject:function ) {...} );
call it as prom(
true
, params)
. Where the params is an array of values that will be applied to onFulfilled callback functions registered by .then()
method.
call it as prom(
false
, params)
. Where the params is an array of values that will be applied to onRejected callbacks registered by .then()
method.
Simply call resolve(result) or reject(error) callback functions from your function body.
The promise()
function returns function-object that has the following methods by itself:
Appends fulfillment and rejection handlers/callbacks to the promise, and returns a new promise. Either one of these handlers will be invoked on promise completion.
Appends rejection handler to the promise, and returns a new promise.
Appends "resolved" handler to the promise, and returns a new promise. This handler will be called on completion of the promise, either successfully or with error.
Static method, returns new promise that will be fulfilled when all arguments(promises) will be fullfiled. If either one of promises is rejected it will rejected too.
The promise.tis also redefines the Element.request() method - http get/post/put/delete call. So with this module included the Element.request() can be called in one of three forms:
.then(onsuccess,onfailure)
call of the promise..then(onsuccess,onfailure)
call of the promise.await
operator allows to awoid using callbacks with promises and write asynchronous logic in linear fashion:
async function getRemoteTexts(url) { try { var text = await view.request { url: url, output: #string }; stdout.println("complete:", text); } catch(e) { stdout.println("got error:", e); } } // starting the task: getRemoteTexts("http://sciter.com/test/text.txt"); stdout.println("task started");
When you will run this code snippet in Sciter you will see following output in console:
> task started > complete: {content of test/text.txt file }
"> task started" will appear first as any function that contains await inside gets automatically converted to Task function.
Invocation of Task function competes immediately and body of the function gets queued for future execution.
When execution hits await statement the task gets postponed until view.request completion (promise resolving).
In other words: await is the way of waiting for promise to be resolved.