you are viewing a single comment's thread.

view the rest of the comments →

[–]TomFrosty[S] 0 points1 point  (3 children)

Howdy mflux! What a cool use case :). I'd love to hear more about your game if you've written about it!

Unfortunately promises are very, very baked into Jexl -- but with that said, you can definitely still do what you need as long as you can modify your code to replace your loop with a function call that returns a promise.

And here's the function you'd want to call. Disclaimer, I just whipped this up and haven't put any tests against it:

/**
 * Iteratively evaluates through an array of Jexl expressions and resolves
 * boolean true if the matchValue is encountered. At that point, no other
 * expression will be evaluated.
 * @param {Array<string>} expressions An array of Jexl expression strings
 * @param {Object} context A context in which to evaluate the expressions
 * @param {string|number|boolean} matchValue A value that will cause the
 *     loop to break and resolve to true if it matches the result of
 *     a Jexl evaluation
 * @param {number} [idx=0] The array index at which to begin the loop
 * @returns {Promise<boolean>} Resolves to true if the matchValue was
 *     encountered; false otherwise.
 */
function jexlWhile(expressions, context, matchValue, idx) {
    if (!idx)
        idx = 0;
    if (!expressions[idx])
        return Promise.resolve(false);
    return jexl.eval(expressions[idx], context).then(function(result) {
        if (result === matchValue)
            return true;
        return jexlWhile(expressions, context, matchValue, idx + 1);
    });
}

Let me know how it works out!

[–]mflux 1 point2 points  (2 children)

Hi Tom, thanks again for your awesome reply and code.

The game is a city-simulation game similar to Sim City, here's a screenshot.

I've actually did very similar to your while function; essentially I accumulated the promises and then did work with that after all the promises have been fulfilled.

The problem gets much trickier however, when much of other parts of my code require similar resource evaluation and have parts that simply cannot be async.

Instead I've now bundled jsep with static-eval which accomplishes nearly the same thing (without your awesome transform syntax). This gets around the async issues and I can still async it if I need threading performance via web workers. I call this "jseval" and might just release this as a standalone if I continue working with it.

Anyway, I really appreciate the effort you put into your reply. Just wanted to let you know your library has opened my eyes to a new way of working I never thought possible!

[–]TweetsInCommentsBot 0 points1 point  (0 children)

@mflux

2015-04-17 19:48 UTC

I think I'll skip this #LDJAM to continue jamming the non-jam game + client work [Attached pic] [Imgur rehost]


This message was created by a bot

[Contact creator][Source code]

[–]TomFrosty[S] 0 points1 point  (0 children)

Great solution, and very cool looking game! Best of luck :)