-
Notifications
You must be signed in to change notification settings - Fork 13
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
compiler: support .then()
and .catch()
#721
Comments
.then()
and .catch()
If I can get this working, we can open up a debate about removing callbacks from all operations except iterators. Which I think would be a HUGE win |
This has taken some working out, and I'm still sure I'm there
Must compile to:
Which boils down to:
Where promisify will take the Operation returned by the operation factory (the function returned by It's hairy and complex. But this has to be compiler magic. I don't want special runtime requirements. |
Ah, Promise.resolve would mean I don't need to create that silly helper: So the long form is:
Which boils down to:
Still a little hairy but it should work. If the operation returns a value, it'll be passed into Promise.resolve, and straight onto the .then If the operation returns a promise, it'll wait for that to return before triggering the then. |
Tricky one this.
Every operation is a promise*. So we should be able to do this in our job code:
This pattern eliminates the need for callbacks. Which is great, because operations with an optional options object and optional callback (which happens a lot) have really ugly signatures.
It also enables this:
Which is badass! Suddenly I can catch any error I want! Which is covered by OpenFn/adaptors#496 (an issue we have with this is that really I want to pass state and error into the catch, like
catch( (e, s) => {})
- but I'm not quite sure how to do that technically. Maybe a custom promise?)The PROBLEM is that the compiler hates this.
The compiler wants to move all top-level operations into an array, like this:
First off I think this breaks everything in the runtime, because that function must be deferred. The .then() will immediately invoke it. So I think the compiler needs to do this?
I don't know, I need to wrap my brain around that.
But secondly, and what this issue is about, is the compiler doesn't recognise
x.then()
as an operation. It recognises it as a method call and ignores it.I think basically the compiler needs to treat
x().then()
andx().catch()
specially and do some kind of magic wrapping around them to make them work properly.The text was updated successfully, but these errors were encountered: