Skip to content

Instantly share code, notes, and snippets.

@zoechi
Created April 10, 2015 10:02
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 zoechi/f6eab34d40c0ee4e4652 to your computer and use it in GitHub Desktop.
Save zoechi/f6eab34d40c0ee4e4652 to your computer and use it in GitHub Desktop.
SO 29331079 yield from within a callback
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