Boost.Cobalt tasks and promises
As we have already seen in this chapter, Boost.Cobalt promises are eager coroutines that return one value and Boost.Cobalt tasks are the lazy version of promises.
We can see them as just functions that don’t yield multiple values like generators do. We can call a promise repeatedly to get more than one value, but the state won’t be kept between calls (as in generators). Basically, a promise is a coroutine that can use co_await
(it can use co_return
too).
Different use cases of promises would be a socket listener to receive network packets, process them, make queries to a database, and then generate some results from the data. In general, their functionality requires asynchronously waiting for some result and then performing some processing on that result (or maybe just returning it to the caller).
Our first example is a simple promise that generates one random number (this can be done with a generator too):
#include <iostream...