Reputation: 100190
I have this code:
function runAsync(fn: Function) {
ret.count++;
fn(function (err: Error) {
err && console.error(err.stack || err);
ret.count--;
if (ret.count < 1) {
ret.cb();
}
});
}
s.on(events.FATAL_TEST_ERROR, function (val: any) {
runAsync(function (cb: Function) {
db.serialize(function () {
db.run('CREATE TABLE lorem (info TEXT)');
let stmt = db.prepare('INSERT INTO lorem VALUES (?)');
for (let i = 0; i < 10; i++) {
stmt.run('Ipsum ' + i);
}
stmt.finalize();
db.all('SELECT rowid AS id, info FROM lorem', function (err: Error, rows: Array<any>) {
cb()
});
});
});
});
what's happening is that if the user wants to include asynchronous calls, they need to call the runAsync function, and then place their code in the function body that's passed to runAsync.
Is there any way I can make this more automatic so that the user has to worry less about conforming perfectly to the API?
My primary concern is that the runAsync function has to be called in the same tick as the call to the event handler. My other concern is that it's all too common for API users to forget to fire callbacks.
Upvotes: 0
Views: 44