1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "utils.h"
6 
7 // These includes are only needed for simulating the passage of time
8 // in a portable manner for the purpose of writing these examples.
9 // You do not need to include these headers just to use |fit::promise|
10 // or |fit::future|.
11 #include <chrono>
12 #include <thread>
13 
14 namespace utils {
15 
sleep_for_a_little_while()16 fit::promise<> sleep_for_a_little_while() {
17     // This is a rather inefficient way to wait for time to pass but it
18     // is sufficient for our examples.
19     return fit::make_promise([waited = false](fit::context& context) mutable {
20         if (waited)
21             return;
22         waited = true;
23         resume_in_a_little_while(context.suspend_task());
24     });
25 }
26 
resume_in_a_little_while(fit::suspended_task task)27 void resume_in_a_little_while(fit::suspended_task task) {
28     std::thread([task]() mutable {
29         std::this_thread::sleep_for(std::chrono::milliseconds(50));
30         task.resume_task();
31     }).detach();
32 }
33 
34 } // namespace utils
35