-
Notifications
You must be signed in to change notification settings - Fork 9
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
TaskBuilder and JsPromise #35
Conversation
This is freaking amazing. |
For high level APIs that reduce complexity and closure nesting, I propose:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple small suggestions, but it's very close!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
A couple of potential issues with this RFC. Lifetime InferenceSince the closure in
The workaround is pretty ugly: fn constrain_callback<F>(f: F) -> F
where
for<'a> F: FnOnce(&mut TaskContext<'a>) -> JsResult<'a, JsString>,
{
f
} This issue is avoided if an owned This is a breaking change and needs to be decided prior to merging. Promises and synchronous exceptionsOne advantage of using fn never_throws_synchronously(mut cx: FunctionContext) {
Ok(JsPromise::try_catch(|cx| {
let n = cx.argument::<JsNumber>(0)?.value(cx);
cx.task(move || n + 42)
.promise(|cx, n| Ok(cx.number(n)))
}))
} The downside of this approach is that it still requires careful code to avoid an Alternatively, we could provide a fn never_throws_synchronously(mut cx: FunctionContext) {
Ok(cx.try_promise(|cx, deferred| {
let n = cx.argument::<JsNumber>(0)?.value(cx);
let channel = cx.channel();
std::thread::spawn(move || {
let n = n + 42;
channel.settle_with(deferred, move |cx| Ok(cx.number(n)));
});
Ok(())
}))
} This is an extension and can be added later without causing any breaking changes. |
@kjvalencik So you mean
Are you talking about a backwards-incompatible change to the existing
This sentence was hard to follow in particular:
|
@dherman I meant a breaking change to These are several different proposals for similar things. The idea is we should make it easy for a user to write a Neon function that always resolved or rejects a promise instead of throwing an error synchronously. The issue I'm having is I haven't been able to come up with a single API that works for both |
After some testing, moving to an owned |
Merged in 2e6c1f1 |
API for executing tasks on the
libuv
threadpool and settling JavaScriptPromise
.Implementation PR: neon-bindings/neon#789