Handle both synchronous and asynchronous queries