1.5Javascript Promises

For every function in the framework which uses an asynchronous callback, there are two versions: one which issues a Promise, and one which takes a more traditional callback function. For example, the code to display a dialog to select an instrument can be written in either of the following ways:

// Traditional version

Framework.SelectInstrument(function(instrumentId) {

// Called asynchronously by the framework when the user selects a market, or cancels

});

// Version using Promise (and optional arrow function)

Framework.pSelectInstrument().then( (instrumentId) => {

// Called asynchronously by the framework when the user selects a market, or cancels

});

The framework uses a simple naming convention where the "standard" version of the function is something like SelectInstrument() and the Promise version has a p prefix such as pSelectInstrument().

Promises in the framework always settle by fulfilling, never by rejecting. For example, a trading request settles by fulfilling regardless of whether it succeeded or failed, with a parameter describing the success or failure result.

There can be subtle differences between the standard and Promise versions of functions. For example - as described in the previous section - RequestCandles() issues multiple, repeated, streaming callbacks as price changes modify the current candle or start a new one. But a Promise can only settle once; it can't have multiple callbacks. Therefore, pRequestCandles() is always treated as a static, non-streaming request.