You should put your data into an object and then pass it around your procs.
Don't create "global" state and capture it in closures.
Here is what I mean (probably doesn't compile, but I hope you get the idea):
import redis, asyncdispatch
type
MyState = ref object
db: AsyncRedis
keystring: string
proc newMyState(): MyState {.async, discardable.} =
const
port = 6379.Port
domain = "127.0.0.1"
return MyState(
db: await openAsync(domain, port)
)
proc rsearch(self: MyState, pattern: string) {.async, discardable.} =
self.keystring = repr(await db.keys(pattern))
self.keystring.echo
let state = newMyState()
waitFor state.rsearch("test")
Run