Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise support in expressions #276

Closed
almousa1990 opened this issue Nov 29, 2020 · 2 comments
Closed

Promise support in expressions #276

almousa1990 opened this issue Nov 29, 2020 · 2 comments

Comments

@almousa1990
Copy link

Hi,

This library support promises in context, however, this support seems to be missing when evaluating an expression

engine
    .parseAndRender('{%if name== blank %}it is promise, not blank!{%endif%}', {name: Promise.resolve('alice')})
    .then(console.log);
@almousa1990
Copy link
Author

almousa1990 commented Nov 29, 2020

Was able to resolve the issue by modifying Expression class, i am not sure if i am correct (yields and generators are new concepts that i am not familiar with). Anyway, this is my slightly modified version of the class:

export class Expression {
  private operands: any[] = []
  private postfix: IterableIterator<Token>

  public constructor (str: string) {
    const tokenizer = new Tokenizer(str)
    this.postfix = toPostfix(tokenizer.readExpression())
  }
  public * evaluate (ctx: Context): any {
    for (const token of this.postfix) {
      if (TypeGuards.isOperatorToken(token)) {
        const r = yield this.operands.pop()
        const l = yield this.operands.pop()
        const result = evalOperatorToken(token, l, r, ctx)
        this.operands.push(result)
      } else {
        this.operands.push(evalToken(token, ctx))
      }
    }
    return this.operands[0]
  }
  public * value (ctx: Context) {
    return toValue(yield this.evaluate(ctx))
  }
}

@harttle
Copy link
Owner

harttle commented Sep 30, 2021

This feature need substantial effort (I tried this too), essentially will change all expression evaluation to async. Also prone to performance issues.

Will leave this issue open to see how many people want this. Feel free to LIKE this.

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

No branches or pull requests

2 participants