I'm trying to read a text file asynchronous line by line but the following code 
fails to compile
    
    
    import asyncdispatch, asyncfile, os
    
    proc main() {.async.} =
      
      var filename = getTempDir() / "test.txt"
      var file = openAsync(filename, fmWrite)
      await file.write("test1\ntest2")
      file.close()
      
      var file = openAsync(filename, fmRead)
      while true:
          let line = await file.readLine()
          if line.len == 0: break
          echo line
      
      file.close()
      removeFile(filename)
    
    waitFor main()
    
    Run

Error messages
    
    
    Exception message: index out of bounds
    Exception type: [IndexError]
    Error: execution of an external program failed: 
'/home/hdias/Documents/Nim-Cookbook/async_openfile '

In Nim is possible make same thing like this with templates?
    
    
    for var line = await file.readLines():
      echo line
    
    Run
    
    
    // Javascript
    for await (const line of readLines(filePath)) {
      console.log(line)
    }
    
    Run

[https://medium.com/@wietsevenema/node-js-using-for-await-to-read-lines-from-a-file-ead1f4dd8c6f](https://medium.com/@wietsevenema/node-js-using-for-await-to-read-lines-from-a-file-ead1f4dd8c6f)

Reply via email to