Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <typeinfo>
#include <functional>
#include <string>
#include <type_traits>
#include <vector>
struct Unit {};
template<typename Result, typename Error>
struct Promise {
template<typename AcceptCB>
auto then(const AcceptCB& accept) -> Promise<typename std::result_of<AcceptCB(Result)>::type, Error> {
acceptCBs.emplace_back(accept);
return Promise<typename std::result_of<AcceptCB(Result)>::type, Error>();
}
template<typename AcceptCB, typename RejectCB>
auto then(const AcceptCB& accept, const RejectCB& reject) ->
Promise<
typename std::result_of<AcceptCB(Result)>::type,
typename std::result_of<RejectCB(Error)>::type
>
{
acceptCBs.emplace_back(accept);
rejectCBs.emplace_back(reject);
return Promise<
typename std::result_of<AcceptCB(Result)>::type,
typename std::result_of<RejectCB(Error)>::type
>();
}
std::vector<std::function<void(Result)>> acceptCBs;
std::vector<std::function<void(Error)>> rejectCBs;
// optional<either<Result, Error>>
bool resolved = false;
bool success = false;
Result result;
Error error;
};
int main() {
Promise<int, std::string> p;
auto pr = p.then(
[](int) {
return 15.0;
}
).then(
[](double) {
return std::string("what");
},
[](const std::string&) {
return 5;
}
);
printf("%s\n", typeid(pr).name());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.