Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created September 9, 2017 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadaustin/4def9614c3c01d05693407cb934223cd to your computer and use it in GitHub Desktop.
Save chadaustin/4def9614c3c01d05693407cb934223cd to your computer and use it in GitHub Desktop.
#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