# Using Async Reader to read user input

**URL:** <https://discuss.ocaml.org/t/using-async-reader-to-read-user-input/4943>\
**Category:** Learning\
**Tags:** async\
**Created:** [January 1, 2020, 12:12pm UTC](https://discuss.ocaml.org/t/using-async-reader-to-read-user-input/4943 "2020-01-01T12:12:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chwee](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@chwee](https://discuss.ocaml.org/u/chwee)\
**Post date:** [January 1, 2020, 12:12pm UTC](https://discuss.ocaml.org/t/using-async-reader-to-read-user-input/4943/1 "2020-01-01T12:12:34Z")

</div>

Hi,

I want to write a function that simple echos a user’s input in stdin, until user enter “exit”.  
My approach is to define a recursive function and use Async’s Reader module to read user input and recursively call the function.

```
let rec read_and_print () : unit Deferred.t =
  let stdin = Lazy.force Reader.stdin in
  print_endline "Enter Message: ";
  Reader.read_line stdin
  >>| function
  | `Eof -> print_endline "error"
  | `Ok "exit" -> ()
  | `Ok x -> 
    print_endline x;
    read_and_print ()

```

However, i get this error:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/ocaml/original/2X/e/ebc9aaede5f36cf176f57b7a7455ef30c3eb2624.png)

How do I solve this?

---

<div class="post-metadata">

**Author:** ![exu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/exu/32/2231_2.png) [@exu](https://discuss.ocaml.org/u/exu)\
**Post date:** [January 1, 2020, 1:21pm UTC](https://discuss.ocaml.org/t/using-async-reader-to-read-user-input/4943/2 "2020-01-01T13:21:16Z")

</div>

The issue is that in your first two cases, you are “returning” a unit, while in the third case, you are returning a unit Deferred.t.

One way to fix this is by forcing the first two cases to return a unit Deferred.t as well. i.e.

print\_endline “error”;  
Deferred.unit

Also, you would need to change your map operator (\>\>|) to bind operator (\>\>=), in order to typecheck.

---

<div class="post-metadata">

**Author:** ![chwee](https://avatars.discourse-cdn.com/v4/letter/c/73ab20/32.png) [@chwee](https://discuss.ocaml.org/u/chwee)\
**Post date:** [January 1, 2020, 1:34pm UTC](https://discuss.ocaml.org/t/using-async-reader-to-read-user-input/4943/3 "2020-01-01T13:34:05Z")

</div>

It works after I changed the code following your suggestions. Thank you so much!
