Created
April 10, 2015 10:02
-
-
Save zoechi/f6eab34d40c0ee4e4652 to your computer and use it in GitHub Desktop.
SO 29331079 yield from within a callback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
class SqlResultSet { | |
List rows; | |
SqlResultSet(this.rows); | |
} | |
Future _runInTxn(txn(Transaction x)) { | |
print('_runInTxn'); | |
return new Future.value(txn(new Transaction())); | |
} | |
class Transaction { | |
Future executeSql( | |
String sql, List args, callBack(Transaction txn, SqlResultSet rs)) { | |
// generate async result sets and complete the Future when done | |
return Future.wait([ | |
new Future.delayed(Duration.ZERO, () => callBack(this, new SqlResultSet( | |
[{'value': 'a'}, {'value': 'b'}, {'value': 'c'}, {'value': 'd'}]))), | |
new Future.delayed(Duration.ZERO, () => callBack(this, new SqlResultSet( | |
[{'value': 'e'}, {'value': 'f'}, {'value': 'g'}, {'value': 'h'}]))), | |
new Future.delayed(Duration.ZERO, () => callBack(this, new SqlResultSet( | |
[{'value': 'i'}, {'value': 'i'}, {'value': 'j'}, {'value': 'k'}]))), | |
]); | |
} | |
} | |
main() async { | |
await for (var value in _all()) { | |
print('value: $value'); | |
} | |
} | |
Stream<String> _all() { | |
var sql = 'SELECT id,value FROM storeName'; | |
var sc = new StreamController(); | |
_runInTxn((txn) { | |
print('in Txn'); | |
return txn.executeSql(sql, [], (txn, rs) { | |
rs.rows.forEach((row) => sc.add(row['value'])); | |
}); | |
}).then((_) { print('close'); sc.close();}); | |
return sc.stream; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment