@dom96 Thanks. The code seems works, but it doesn't. db.close() seems executed before the code 'block', this is the error message: asyncdispatch.nim(1492) waitFor asyncdispatch.nim(1496) poll asyncdispatch.nim(292) runOnce Error: unhandled exception: No handles or timers registered in dispatcher. [ValueError]
So I must use the following code: template withDb(body: untyped) = let dbFut = open(...) yield dbFut let db = dbFut.read() block: body close(db) But the code above cannot deal with the exception raised from 'body', the code 'body' raise a exception, close(db) will not be executed. I cannot use try...finally either, the compiler will not pass. So there is a really big problem, how to close the resource reliablely within a async proc?