Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    [Small how-to] Eliminate callbacks!

    I thought I'd share this since I absolutely love it. Our customers are all on Chrome/FF/Edge so we can target 'modern' browsers. We wanted to eliminate callbacks and decided to try async functions - https://developer.mozilla.org/en-US/...async_function

    Here's an example of what we did (the snippet is for the performCustomOperation on DataSource. First, after the ISC framework has loaded but before loading any of your own datasources:
    Code:
    isc.DataSource.addProperties({
        performCustomOperationAsync: function(operationId, data, requestProperties) {
            requestProperties = requestProperties || {};
            requestProperties.willHandleError = true;
            return new Promise(function(resolve, reject) {
                function cb(dsResponse, data, dsRequest) {
                    if (dsResponse.status < 0) {
                        reject(dsResponse);
                    }
                    else if ( requestProperties && requestProperties.dataArity === "single" && Array.isArray(data) && data.length > 0) {
                        if ( data.length > 1 ) {
                            this.logWarn(`Request properties contained dataArity 'single' but remote server returned ${data.length} results - returning first.`);
                        }
                        resolve(data[0]);
                    }
                    else {
                        resolve(data);
                    }
                }
                this.performCustomOperation(operationId, data, cb, requestProperties);
            }.bind(this));
        }
    });
    Then, to use:

    Code:
    [B]async[/B] function myFunc() {
        try {
            let loginBeginData = [B]await[/B] AuthDS.performCustomOperationAsync("begin", inputRecord, {dataArity: "single"});
            isc.say(JSON.stringify(loginBeginData));
        }
        catch (dsResponse) {
            // TODO: Log error and possibly retry
        }
    }
    The callbacks are gone and we can write the statement as if it executes inline
    Last edited by jaredm; 17 Jul 2017, 13:58.
Working...
X