feat: handle promise arrays

pull/291/head
med8bra 3 months ago
parent 19520743f7
commit a730431127

@ -51,7 +51,7 @@ async function runTransforms(json, args, theme) {
let i, code, jsCode, output = json
for ([i, code] of args.entries()) try {
jsCode = transpile(code)
const fn = `(function () {
const fn = `(async function () {
const x = this
return ${jsCode}
})`
@ -109,19 +109,19 @@ function transpile(code) {
if (/^map\(.+?\)$/i.test(code)) {
let s = code.substring(4, code.length - 1)
if (s[0] === '.') s = 'x' + s
return `x.map((x, i) => apply(${s}, x, i))`
return `Promise.all(x.map((x, i) => apply(${s}, x, i)))`
}
if (/^@/.test(code)) {
const jsCode = transpile(code.substring(1))
return `x.map((x, i) => apply(${jsCode}, x, i))`
return `Promise.all(x.map((x, i) => apply(${jsCode}, x, i)))`
}
return code
}
async function run(json, code) {
const fn = eval(code).call(json)
const fn = await eval(code).call(json)
return apply(fn, json)

@ -211,4 +211,28 @@ void async function main() {
const {stdout} = await runSimple(`.name package.json`)
t.equal(stdout, 'fx\n')
})
await test("promises - promise output", async (t) => {
const { stdout } = await run(
[{ greeting: "hello world" }],
"'Promise.resolve(x)'",
);
t.deepEqual(stdout, '[\n {\n "greeting": "hello world"\n }\n]\n');
});
await test("promises - map promises", async (t) => {
const { stdout } = await run(
["foo", "bar"],
"'map(x=>Promise.resolve(x))'",
);
t.deepEqual(JSON.parse(stdout), ["foo", "bar"]);
});
await test("promises - nested promises", async (t) => {
const { stdout } = await run(
["foo", "bar"],
"'map(async x=>({name: await Promise.resolve(x)}))'",
);
t.deepEqual(JSON.parse(stdout), [{ name: "foo" }, { name: "bar" }]);
});
}()

Loading…
Cancel
Save