You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/tasks.md
+42Lines changed: 42 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -259,3 +259,45 @@ $execute {
259
259
```
260
260
261
261
Note that if your code needs to interact with the Cocos2d UI **at all**, then you should probably be using an `EventListener` in some node or a manager class instead.
262
+
263
+
## Chaining tasks
264
+
265
+
You can chain tasks by using `Task::chain`. This allows you to take the result of a task once it finished and run another task right after, based on that return value. \
266
+
Progress updates are only sent from the last Task in the chain, for simplicity
Tasks can be used in [C++20 coroutine](https://en.cppreference.com/w/cpp/language/coroutines) functions, easily allowing for multiple asynchronous calls to happen within the same code. Note that this may have a little performance overhead compared to regular Task code.
281
+
282
+
```cpp
283
+
Task<int> someTask() {
284
+
auto response = co_await web::WebRequest().get("https://example.com");
285
+
co_return response.code();
286
+
}
287
+
```
288
+
289
+
There are a few specific things you should be aware of when using this syntax:
290
+
* The body of the coroutine is ran in whatever thread it got called in
291
+
* If the task the coroutine is waiting on is cancelled, the whole coroutine is cancelled
292
+
* If the task returned by the coroutine is cancelled, any pending task that is running is cancelled
293
+
294
+
You can also send progress values using `co_yield`
0 commit comments