This question already has an answer here:
- Waiting for Futures raised by other Futures 1 answer
I'm trying to convert the following javascript/jquery code into dart, but I'm having problems understanding how futures work.
function fnA() {
fnB().then(function() {
// do something
}
}
function fnB() {
var ret = $.Deferred();
_db.open(database_name).then(function() {
var defers = [];
_db.keys().forEach(function(key_name) {
var key_dfd = $.Deferred();
defers.push(key_dfd);
_db.getByKey(key_name).then(function(data) {
key_dfd.resolve(data);
});
});
$.when(defers).then(ret.resolve);
})
.fail(function() {
ret.reject();
});
return ret.promise();
}
I thought I could do it similar to following, but it doesn't seem to work:
(I'm using lawndart for the database)
void fnA() {
fnB().then((_) {
// Here, all keys should have been loaded
});
}
Future fnB() {
return _db.open().then((_) {
return _db.keys().forEach((String key_name) {
return _db.getByKey(key_name).then((String data) {
// do something with data
return data;
});
});
});
}