punchd/asyncreadline.nim

19 lines
588 B
Nim
Raw Normal View History

2020-05-08 19:34:37 +02:00
## 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