{"action":"created","issue":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/3893","repository_url":"https://api.github.com/repos/nim-lang/Nim","labels_url":"https://api.github.com/repos/nim-lang/Nim/issues/3893/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/Nim/issues/3893/comments","events_url":"https://api.github.com/repos/nim-lang/Nim/issues/3893/events","html_url":"https://github.com/nim-lang/Nim/issues/3893","id":135426612,"number":3893,"title":" Feature request: Async wait with timeout","user":{"login":"cheatfate","id":5955411,"avatar_url":"https://avatars.githubusercontent.com/u/5955411?v=3","gravatar_id":"","url":"https://api.github.com/users/cheatfate","html_url":"https://github.com/cheatfate","followers_url":"https://api.github.com/users/cheatfate/followers","following_url":"https://api.github.com/users/cheatfate/following{/other_user}","gists_url":"https://api.github.com/users/cheatfate/gists{/gist_id}","starred_url":"https://api.github.com/users/cheatfate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cheatfate/subscriptions","organizations_url":"https://api.github.com/users/cheatfate/orgs","repos_url":"https://api.github.com/users/cheatfate/repos","events_url":"https://api.github.com/users/cheatfate/events{/privacy}","received_events_url":"https://api.github.com/users/cheatfate/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-02-22T14:07:52Z","updated_at":"2016-02-22T15:00:17Z","closed_at":null,"body":"Currently there no any functions to wait async procedure with timeout.\r\nI propose to add function like this (may be somebody can make it better)\r\n```\r\nproc waitOrTimeout*[T](fut1: Future[T], timeout: int): Future[void] =\r\n  ## Wait till future ``fut1`` complete or timeout has been reached (in milliseconds)\r\n  ## \r\n  var retFuture = newFuture[void](\"async.waitOrTimeout\")\r\n  var fut2 = sleepAsync(timeout)\r\n  proc cb() =\r\n    if not retFuture.finished: retFuture.complete()\r\n  fut1.callback = cb\r\n  fut2.callback = cb\r\n  return retFuture\r\n```\r\n\r\nSo you can easily wait for async procedure like this\r\n\r\n```\r\nawait waitOrTimeout(future, 3000)\r\nif future.finished:\r\n  # future finished\r\n  discard\r\nelse:\r\n  # timeout reached\r\n  discard\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/comments/187219858","html_url":"https://github.com/nim-lang/Nim/issues/3893#issuecomment-187219858","issue_url":"https://api.github.com/repos/nim-lang/Nim/issues/3893","id":187219858,"user":{"login":"dom96","id":246651,"avatar_url":"https://avatars.githubusercontent.com/u/246651?v=3","gravatar_id":"","url":"https://api.github.com/users/dom96","html_url":"https://github.com/dom96","followers_url":"https://api.github.com/users/dom96/followers","following_url":"https://api.github.com/users/dom96/following{/other_user}","gists_url":"https://api.github.com/users/dom96/gists{/gist_id}","starred_url":"https://api.github.com/users/dom96/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dom96/subscriptions","organizations_url":"https://api.github.com/users/dom96/orgs","repos_url":"https://api.github.com/users/dom96/repos","events_url":"https://api.github.com/users/dom96/events{/privacy}","received_events_url":"https://api.github.com/users/dom96/received_events","type":"User","site_admin":false},"created_at":"2016-02-22T15:00:17Z","updated_at":"2016-02-22T15:00:17Z","body":"There is, use ``sleepAsync``:\r\n\r\n```nim\r\nawait future or sleepAsync(3000)\r\nif future.finished:\r\n  # future finished\r\n  discard\r\nelse:\r\n  # timeout reached\r\n  discard\r\n```"}}