Closed
Description
From what I can tell #276 has been solved and just hasn't been closed yet (correct?). However, I would like to use arbitrary nested Promises. I think these test cases demonstrate it better than I could with words
import { Liquid } from 'liquidjs';
(async () => {
const templateEngine = new Liquid();
const context = {
a: 1,
b: Promise.resolve(1),
async c() {
return 1;
},
d: {
d: 1,
},
e: {
e: Promise.resolve(1),
},
f: {
async f() {
return 1;
},
},
g: Promise.resolve({
g: 1,
}),
async h() {
return {
h: 1,
};
},
i: Promise.resolve({
i: Promise.resolve(1),
}),
j: Promise.resolve({
async j() {
return 1;
},
}),
};
console.log(await templateEngine.evalValue('a == 1', context), true);
console.log(await templateEngine.evalValue('b == 1', context), true);
console.log(await templateEngine.evalValue('c == 1', context), true);
console.log(await templateEngine.evalValue('d.d == 1', context), true);
console.log(await templateEngine.evalValue('e.e == 1', context), true);
console.log(await templateEngine.evalValue('f.f == 1', context), true);
console.log(await templateEngine.evalValue('g.g == 1', context), true);
console.log(await templateEngine.evalValue('h.h == 1', context), true);
console.log(await templateEngine.evalValue('i.i == 1', context), true);
console.log(await templateEngine.evalValue('j.j == 1', context), true);
console.log(await templateEngine.parseAndRender('{{a}}', context), '1');
console.log(await templateEngine.parseAndRender('{{b}}', context), '1');
console.log(await templateEngine.parseAndRender('{{c}}', context), '1');
console.log(await templateEngine.parseAndRender('{{d.d}}', context), '1');
console.log(await templateEngine.parseAndRender('{{e.e}}', context), '1');
console.log(await templateEngine.parseAndRender('{{f.f}}', context), '1');
console.log(await templateEngine.parseAndRender('{{g.g}}', context), '1');
console.log(await templateEngine.parseAndRender('{{h.h}}', context), '1');
console.log(await templateEngine.parseAndRender('{{i.i}}', context), '1');
console.log(await templateEngine.parseAndRender('{{j.j}}', context), '1');
})();
Once Liquid has seen a promise or a property that was a promise, it won't resolve promises of properties that follow after that. I hope this is an easy fix and it just takes a wrong branch.
I need this because I'm using Liquid in an Electron app for data processing. And the context I pass in is lazy, because nobody will need all the details (including large buffers) for every template. So I only want to load the data that is needed for the current processing pipeline.
Activity
LiquidExpression
class orevalExpression
to evaluate single expressions #527[-]Nested Promise support[/-][+]Nested Promise support for scope object[/+]feat: promise in expression & nested property, #533 #276
harttle commentedon Aug 27, 2022
This is a very similiar issue with #276. Thanks to almousa1990 we solved a part of the problem: promise for Liquid drops.
After #285 is merged, it seems that I forgot this issue for a long time. Thank you for providing a thorough test case list, and share your use case for this feature. One main concern is performance, but it seems OK in my local branch:
Since old APIs like
evalToken
andContext.get
still work fine (test cases not broken), I'm releasing this feature in a minor version.chore(release): 9.42.0 [skip ci]
Prinzhorn commentedon Aug 29, 2022
From what I can tell this does everything I ever wanted. So thank you very much! I'll let you know once I actually start implementing most of this on my end and share a link to the product if you're interested.