Skip to content

Please document callback this and asynchronous behavior #1408

Open
@rhansen

Description

@rhansen

Looking at the code it is apparent that the executions of the callbacks passed to the constructor and close() method (and probably other methods too; I haven't looked) have these properties:

  • They are executed asynchronously. In other words, the callbacks won't start running until the constructor or close() function has already returned. Knowing this is useful because it means this code is guaranteed to work:

    const db = await new Promise((resolve, reject) => {
      const db = new sqlite3.Database(filename, (err) => {
        if (err != null) return reject(err);
        resolve(db); // `db` is guaranteed to be defined by this point.
      });
    });
  • If the callback is a regular function (not an arrow function) then the bound context (the this keyword) is guaranteed to be the Database object. Knowing this is useful because it means this code is guaranteed to work:

    const db = await new Promise((resolve, reject) => {
      new sqlite3.Database(filename, function (err) {
        if (err != null) return reject(err);
        resolve(this); // `this` is guaranteed to be the Database object.
      });
    });

Please document these behaviors. I would update the API wiki page myself, but the wiki is not open for editing. See: rhansen/node-sqlite3-wiki@56e46c0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions