punchd/asyncreadline.nim

19 lines
588 B
Nim

## asyncReadline as discussed at https://github.com/nim-lang/Nim/issues/11564
import asyncdispatch, threadpool
proc asyncReadline*(): Future[string] =
let event = newAsyncEvent()
let future = newFuture[string]("asyncReadline")
proc readlineBackground(event: AsyncEvent): string =
result = stdin.readline()
event.trigger()
let flowVar = spawn readlineBackground(event)
proc callback(fd: AsyncFD): bool =
future.complete(^flowVar)
true
addEvent(event, callback)
return future