| 78 | |
| 79 | template <class F> |
| 80 | auto execAsync(const F& func, TaskPriority priority = TaskPriority::DefaultOnMainThread) |
| 81 | -> Future<decltype(func())> { |
| 82 | if (g_network->isSimulated()) { |
| 83 | return map(delayJittered(meanDelay), [func](Void _) { return func(); }); |
| 84 | } |
| 85 | Promise<decltype(func())> promise; |
| 86 | addTask([promise, func, priority] { |
| 87 | try { |
| 88 | auto funcResult = func(); |
| 89 | onMainThreadVoid([promise, funcResult] { promise.send(funcResult); }, priority); |
| 90 | } catch (Error& e) { |
| 91 | TraceEvent("ErrorExecutingAsyncTask").error(e); |
| 92 | onMainThreadVoid([promise, e] { promise.sendError(e); }, priority); |
| 93 | } |
| 94 | }); |
| 95 | return promise.getFuture(); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | #endif |
no test coverage detected