-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
yield / await #53
Comments
It should already expand to the last code block. |
Can you point me where in the proposal this is defined? |
I think you would need parenthesis to achieve what you want. If written this way... a |> yield b |> c ...then it would be interpreted as However with explicit parenthesis I think it could work: a |> (yield b) |> c The problems is that this solution becomes less apparent when broken up by newlines: input
|> yield b //<-- Needs parens!
|> f |
Wow thanks, that's even less intuitive that what I expected. So my first example would be function* g() {
const f = yield (bar(foo));
f("input");
}
async function a() {
const f = await (bar(foo));
f("input");
} And the example in #31 would absolutely not behave as expected. I assume we could rely on linters to discourage the use of |
I should make clear my comment is just a guess. I don't know how to read proposal grammars, so I can't verify if a spec like #51 behaves this way or not. |
The spec in #51 puts |
const addOne = (x) => Promise.resolve(x + 1);
const double = (x) => Promise.resolve(x * 2);
(async () => {
const foo = await (1
|> async x => await addOne(x)
|> async x => await double(x)
);
console.log(foo); // 4
})(); This works (repl) but it leaves a bit to be desired. Sounds like we'd need additional syntax to clean it up. Something like this could be nice: const foo = await (1
|> await with addOne
|> await with double
);
// loosely
Promise.resolve(1)
.then(addOne)
.then(double)
.then(foo => {}) |
Potentially partial application with const foo = await (1
|> addOne(await?)
|> double(await?)
); |
@azz interesting idea. But it's a bit complicated, as I'm not sure what happens with that function as a value. How can it block? I think we'd need something which is not a rectified function. To clarify, in the current spec text, even if you put in parens, yield and await won't do what you expect--they would refer to the function, not what was passed in. You just have to break out of the pipeline chain for now. |
If const foo = 1
|> addOne
|> await
|> double
|> await;
// or...
const foo = 1
|> addOne |> await
|> double |> await; A bit annoying, but it would work. |
Why not allow And the same for I would think this is the most natural use of those keywords from within a |
I agree it would be better, I just don't know how complicated it would make the proposal. |
What if let double = Promise.resolve(x => x + x);
await (1 |> await double); // 2 |
That should be invalid. Pipeline operator should only allow |
Oh, my intuition was it was any expression, i.e.: 1 |> await double; would be (await double)(1); |
The expression after a |
@azz Seems like a complicated way of just doing |
I've created a pull request for |
I guess we can close this, now everything is answered in #66 |
For checking, since all information I found in other issues are more than 18 months old, about the
yield
andawait
keywords:The following functions:
Would be equivalent to:
And there would be no easy way to do the following using the pipeline operator:
The text was updated successfully, but these errors were encountered: