Skip to content

Instantly share code, notes, and snippets.

@narqo
Last active December 29, 2015 00:38
Show Gist options
  • Save narqo/bf7debec89a760e6b007 to your computer and use it in GitHub Desktop.
Save narqo/bf7debec89a760e6b007 to your computer and use it in GitHub Desktop.
Yet another concept of async BEMTREE templates
block('global').def(function() {
var data = this.param('req'),
host = this.param('apiHost'),
params = {};
return async(function() {
try {
var result = await(this.httpInvoke({ host : host, path : '/some/api' });
var status = result.status;
if(!status) {
throw new Error('unknown request!');
}
params.login = result.login;
params.info = result.info;
} catch(e) {
console.error('Something happend!', e);
}
this.ctx.params = params;
return applyNext();
});
});
@veged
Copy link

veged commented Nov 21, 2013

напиши примерно, во что это должно скомпилироваться?

@narqo
Copy link
Author

narqo commented Nov 21, 2013

Это должен быть аналог текущей реализации на промисах:

block('global').def(function() {
    var data = this.param('req'),        // ← представим, что это `BEMContext#param()`, по аналогии с i-bem.js
        host = this.param('apiHost'),
        params = {};

    return this.doAsync(function() {
            return this.httpInvoke({ host : host, path : '/some/api' })    // ← какой-нибудь `BEMContext#httpInvoke()`
        })
        .then(function(result) {
            var status = result.status;
            if(!status) {
                throw new Error('unknown request!');
            }

            params.login = result.login;
            params.info = result.info;

            return params;
        })
        .fail(function(e) {
            console.error('Something happend!', e);
            return {};
        })
        .then(function(params) {
            this.ctx.params = params;

            return applyNext();
        });
});

про проблемы с контекстами я (долго) не думал пока >_>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment