Simplify with yield
Sample use
var Dexie = require('dexie');
var async = Dexie.async,
spawn = Dexie.spawn;
var db = new Dexie('testdb');
db.version(1).stores({
people: '++id,name,age',
assets: '++id,ownerId,assetName'
});
spawn(function*() {
//
// Write your synchronous-like code within a spawn() block
//
// Add a person:
var id = yield db.people.add({name: 'Foo', age: 34});
// Add two assets that links to the newly added person:
yield db.assets.bulkAdd([
{assetName: "car", ownerId: id},
{assetName: "house", ownerId: id}
]);
// Now, list all people and their assets:
var people = yield db.people.toArray();
for (var i=0; i<people.length; ++i) {
var assets = yield db.assets
.where('ownerId').equals(people[i].id)
.toArray();
console.log(`${people[i].name}'s assets: ${assets.map(a => a.assetName).join(',')}`);
}
}).then(function() {
//
// spawn() returns a promise that completes when all is done.
//
console.log("Complete");
}).catch(function(e) {
//
// If any error occur in DB or plain exceptions, you
// may catch them here.
//
console.error("Failed: " + e);
});Use in db.transaction()
async
spawn
Calling Sub Functions
Method 1
Method 2
How this maps to ES7 async / await
Using function*() and yield
Using async / await