Chaining Dart futures - possible to access intermediate results?
You can use a nested assignment to avoid curly braces and return
:
.then((RedisClient rc) => (redisClient = rc).exists(indexKey))
You can do scopes with futures too, by not putting all the 'then' calls at the same level.I'd do something like:
Future<String> FirstValue(String indexKey) => RedisClient.connect(Config.connectionStringRedis) .then((RedisClient redisClient) => redisClient.exists(indexKey) .then((bool exists) => !exists ? null : redisClient.smembers(indexKey)) .then((Set<String> keys) => redisClient.get(keys.first)) .then((String value) => "result: $value"); );
Indentation is always difficult with code like this. This example follows the Dart style guide, but I think it could be more readable with less indentation of the then
calls:
Future<String> FirstValue(String indexKey) => RedisClient.connect(Config.connectionStringRedis) .then((RedisClient redisClient) => redisClient.exists(indexKey) .then((bool exists) => !exists ? null : redisClient.smembers(indexKey)) .then((Set<String> keys) => redisClient.get(keys.first)) .then((String value) => "result: $value"); );