Skip to content

Instantly share code, notes, and snippets.

@rsirres
Created March 24, 2017 21:38
Show Gist options
  • Save rsirres/9949a4b98b7d5b9dd441483f14f06fbf to your computer and use it in GitHub Desktop.
Save rsirres/9949a4b98b7d5b9dd441483f14f06fbf to your computer and use it in GitHub Desktop.
Nimlang Nim await example
import os, asyncnet, asyncdispatch
proc callback1(): Future[int] =
var retFuture = newFuture[int]("callback1")
echo "Callback1: Waiting"
sleep(3000)
retFuture.complete(10)
return retFuture
proc callback2(): Future[int] =
var retFuture = newFuture[int]("callback2")
echo "Callback2: Waiting"
sleep(3000)
if 0.7 < 0.5:
retFuture.complete(5)
else:
retFuture.fail(newException(OSError, "Error occured"))
return retFuture
proc main() {.async.} =
try:
var value = await callback1()
var value2 = await callback2()
echo value * value2
except:
echo "Operation Failed"
waitFor main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment