Hello, I'm starting an application (that I hope to become a mail client using 
the JMAP protocol), and I'm trying the fidget GUI toolkit. I have a problem 
with it, however, I can't make async futures work.

I can't make use of built-in fidget HTTP helper, as I need to make POST 
requests instead of GET requests, so I instantiated an `AsyncHttpClient` 
object. However, when I make the async call, I never get in the future 
callback, or past the `await` in the async proc.

The async proc looks like this ( 
[jmap/transport.nim](https://github.com/mildred/nim-jmapmail/blob/fcd5da57cd8ccd58084236bdf3adc6e12f323244/src/jmap/transport.nim#L48-L71)):
    
    
    proc request*(t: Transport, req: sink Request): Future[Response] {.async.} =
      var gen: Generator
      for meth in items(req.methodCalls):
        if meth.id != "": gen.incl(meth.id)
      for meth in items(req.methodCalls):
        if meth.id == "": meth.id = gen.id()
      
      let body = req.toJson
      echo &"request body = {body}"
      let fut = t.http.post(t.url, body = body)
      fut.addCallback do():
        echo "callback ok"
      let resp = await fut
      echo "got response headers"
      let json = await(resp.body)
      echo &"got response body = {json}"
      
      var response: Response = json.fromJson(Response)
      return response
    
    
    Run

and I never get past the "request body =" message.

The future is called in 
[actions/login.nim](https://github.com/mildred/nim-jmapmail/blob/fcd5da57cd8ccd58084236bdf3adc6e12f323244/src/actions/login.nim#L19-L23),
 it's another async procedure with an await. The async procedure is called from 
a `onClick` fidget callback in 
[ui/login.nim](https://github.com/mildred/nim-jmapmail/blob/fcd5da57cd8ccd58084236bdf3adc6e12f323244/src/ui/login.nim#L38-L40):
    
    
    group "button":
            box 140, 110, 100, 30
            button("Login")
            onClick:
              asyncCheck refreshAfter(login(v.config, v.account))
              v.login_progress = true
    
    
    Run

with the `refreshAfter` procedure, that is supposed to hook up the future with 
fidget as below (in 
[ui/common.nim](https://github.com/mildred/nim-jmapmail/blob/fcd5da57cd8ccd58084236bdf3adc6e12f323244/src/ui/common.nim#L48-L54)):
    
    
    proc refreshCallback() =
      echo "refreshCallback"
      fidget.refresh()
    
    proc refreshAfter*[T](future: Future[T]): Future[T] =
      future.addCallback(refreshCallback)
      return future
    
    
    Run

I'm not sure what I'm doing wrong here. I suppose the future is somehow not 
registered with the event loop, but I don't know how to do that. I also noticed 
that I could get past the first callback if I replaced all the `async` with 
`waitFor` which I did by mistake at first.

If you want to test the code, you can trigget this by running the app and 
trying to login with any e-mail `@mildred.fr` or `@jmap.io`.

Thank you

Reply via email to