diff --git a/.vscode/settings.json b/.vscode/settings.json index aaed338..8372c4a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -69,6 +69,8 @@ "cinttypes": "cpp", "typeindex": "cpp", "typeinfo": "cpp", - "variant": "cpp" + "variant": "cpp", + "coroutine": "cpp", + "source_location": "cpp" } } \ No newline at end of file diff --git a/lib/asynco.hpp b/lib/asynco.hpp index d57ec0f..f06a492 100644 --- a/lib/asynco.hpp +++ b/lib/asynco.hpp @@ -35,6 +35,28 @@ T await_(future&& r) { return move(r).get(); } +/** + * Block until the asynchronous call completes or time expired +*/ +template +T await_(future& r, uint64_t time) { + if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) { + throw runtime_error("Asynchronous execution timed out"); + } + return r.get(); +} + +/** + * Block until the asynchronous call completes or time expired +*/ +template +T await_(future&& r, uint64_t time) { + if (r.wait_for(chrono::milliseconds(time)) == std::future_status::timeout) { + throw runtime_error("Asynchronous execution timed out"); + } + return move(r).get(); +} + } }