Promise.then question

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Promise.then question

jlorenzen
If I'm doing something like the following:

ctx.getRequest().getBody()
    .map(typedData -> typedData.getText(UTF_8))
    .then(this::someBlockingMethod)

Will the call to `someBlockingMethod` block the main thread if it has some IO to perform or will the Promise.then execute that logic in a separate thread?

Thanks,
James Lorenzen
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Promise.then question

Luke Daley
Administrator
Hi James,

You need to be explicit about blocking operations. Ratpack can't detect them ahead of time. Here's how you would do it:

ctx.getRequest().getBody()
    .map(typedData -> typedData.getText(UTF_8))
    .blockingOp(this::someBlockingMethod)
    .then(text -> ctx.render("ok"))

If your blocking operation returns a result, use blockingMap() instead of blockingOp().

The function given to .next() will _always_ execute on a compute thread.
Loading...