Sunday, 2 April 2017

Include required API contract calls automatically

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?



via Alexander Mills

No comments:

Post a Comment