silentmatt / expr-eval

Mathematical expression evaluator in JavaScript
http://silentmatt.com/javascript-expression-evaluator/
MIT License
1.18k stars 239 forks source link

Asycnhronous evaluation #225

Open m-nab opened 4 years ago

m-nab commented 4 years ago

Hi,

Would it be possible to add a Asynchronous evaluator ? I am currently using this libray to call a couple of functions dynamically, however I do have a a couple of functions which are asynchronous in nature. Since the evaluation happens instantly, it immediately resolves the pending promise instead of the value.

So the case would be something like this;

Parser.functions.AsyncCall1 = async function() { const res  = await api.getAmount(); return res; }
Parser.functions.AsyncCall2 = async function() { const res  = await api.getAmount(); return res; }

Parser.evaluateAsync('AsyncCall1() + AsyncCall2()');

Although I do realize this was not the intended use-case.

silentmatt commented 4 years ago

I'm not sure this would be possible without duplicating essentially the whole evaluator. I agree it would be useful though, so I'll give it some thought.

j2L4e commented 3 years ago

Made async work in https://github.com/j2L4e/expr-eval/tree/async It passes the original test suite but as you said, it basically required copying the whole thing, so I just changed it to all-async for now.

edit: also minification is broken right now

strykeio commented 3 years ago

This would be extremely useful for us too. Currently, we are having to pre-process formulas that may include async functions, execute the async functions, replace their result in the formula, and then evaluate it with expr-eval.

gkjohnson commented 1 year ago

Just want to chime in that this would be a great addition though it would be nice to be able to retain both a synchronous and asynchronous code paths - which might be a bit complicated as already mentioned.

My use case is to remotely load a YAML file with parameters to parse into a dictionary which is then used in the expression eval for this robotics xacro format parser: https://github.com/gkjohnson/xacro-parser.

mdtanveer commented 2 months ago

This would be a very useful enhancement. My use case is to fetch stock quotes and be able to do calculations on their prices.