promise-cpp
promise-cpp copied to clipboard
promise::all(...) cannot resolve to native vector
Greetings, I have the following code:
std::vector<Promise> promise_list = {
promise::resolve<float>(10),
promise::resolve<float>(11),
};
all(promise_list).then([](const std::vector<float> &vals){
/* code here for all promise objects are resolved */
}).fail([](){
/* code here for one of the promise objects is rejected */
});
This compiles but bails with a bad any_cast execption. The following though works:
std::vector<Promise> promise_list = {
promise::resolve<float>(10),
promise::resolve<float>(11),
};
all(promise_list).then([](const std::vector<any> &vals){
std::vector<float> res{};
std::transform(vals.begin(), vals.end(), std::back_inserter(res),
[](const promise::any &r) { return r.cast<float>(); });
return res;
}).then([](const std::vector<float> &vals){
/* code here for all promise objects are resolved as floats */
}).fail([](){
/* code here for one of the promise objects is rejected */
});
I am wondering if you can use the container std::vector<T>::value_type for the cast from the promise into the function. I am not sure if this would break other stuff though since it would need to happen after resolution. This would also break instances where you have multiple types returned by the promises. Thoughts?
hi, currently the resolved type of all() is vector<any>, which is not converted to vector<T> automatically.
Yeah, I was wondering how hard that behavior would be to change. I'm not a huge fan of type-erasure, but for 99% of the stuff it doesn't matter, and it does make things a bit easier. It was just this one that throwing the bad cast was unexpected.