You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking to start using promises to remove the callback hell I've accumulated in my application, but I need to be able to ensure that a promise is run in a specific thread pool.
I had thought that DefaultDeferredManager.when(Promise) would do just that, but in fact it does nothing.
In theory it should, but it doesn't appear to work as expected. I would expect this to print 'got result' on a different thread to the one that prints 'resolving', but they execute on the same thread, and a stack trace shows both methods on the stack. The situation I would need to use this in would requires that I don't process the callbacks in the thread that's doing the resolving, I have another thread pool for that.
public static void main(String[] args) throws Exception
{
ExecutorService executorService = Executors.newCachedThreadPool();
DeferredManager dm = new DefaultDeferredManager(executorService);
Promise<String, Exception, Void> p = createPromise();
dm.when(p).done((String result) -> {
System.out.println(Thread.currentThread().getName() + ": got result: " + result);
});
}
public static Promise<String, Exception, Void> createPromise()
{
final DeferredObject<String, Exception, Void> p = new DeferredObject<>();
new Thread(() -> {
try
{
Thread.sleep(5000);
System.out.println(Thread.currentThread().getName() + ": resolving");
p.resolve("hello");
}
catch (Throwable t)
{
}
}).start();
return p.promise();
}
Hi
I was looking to start using promises to remove the callback hell I've accumulated in my application, but I need to be able to ensure that a promise is run in a specific thread pool.
I had thought that DefaultDeferredManager.when(Promise) would do just that, but in fact it does nothing.
I was thinking you'd need something like this:
Regards
Stephen
The text was updated successfully, but these errors were encountered: