antonmedv / fx

Terminal JSON viewer & processor
https://fx.wtf
MIT License
18.94k stars 440 forks source link

feat: handle promise arrays #291

Closed med8bra closed 5 months ago

med8bra commented 6 months ago

What

Currently, fx handles only promise value if it is an output.

This PR improves on that to handle also array of promises and support using await in expressions. Which will allow users to perform async operations inside map (.i.e: map(x => fetch(x)) ).

Before

❯ echo '[1,2,3]' | fx 'map(x=>Promise.resolve(x))'
[
  {},
  {},
  {}
]

After

❯ echo '[1,2,3]' | ./fx 'map(x=>Promise.resolve(x))'
[
  1,
  2,
  3
]

Ideas

Maybe fx could introduce a syntax for promises, but I really like the simplicity of fx and offloading everything to js runtime, which reduces parse/eval complexity.

med8bra commented 6 months ago

Hi @antonmedv, what do you think about this one?

antonmedv commented 6 months ago

But you can already use Promise.all:

echo '[1,2,3]' | fx 'Promise.all(x.map(x=>Promise.resolve(x)))'
med8bra commented 6 months ago

That's true. And since fx(Promise.resolve(x)) = x it would be good UX to have also fx([Promise.resolve(x)]) = [x].

antonmedv commented 5 months ago

As this is easily done with:

echo '[1,2,3]' | fx '.map(x=>Promise.resolve(x))' 'Promise.all(x)'

Let's not merge this PR for now.