Skip to content

Sync vs Async initialisation #12

@oliverbrooks

Description

@oliverbrooks

I'm wondering if it is easier to initialise sql-stamp by loading all the files synchronously.

For example:

var sqlStamp = require("sql-stamp");

// Make some templates
var templates = sqlStamp(["/path/to/sql"]); // This step sync

// Use those templates
var sql = templates("/path/to/sql", {
  one: "fish"
});

The advantage of this is you could easily put the template loading part into a module and require it.

/sql-templates.js

var sqlStamp = require("sql-stamp");

// Make some templates
module.exports = sqlStamp(["/path/to/sql"]); // This step sync

/model.js

var templates = require("./sql-templates");

// Use those templates
var sql = templates("/path/to/sql", {
  one: "fish"
});

// use sql

With async you need some kind of initialising step to ensure all the files are loaded.

/sql-templates.js

var sqlStamp = require("sql-stamp");

// Make some templates
var templates;
function init () {
  return sqlStamp(["/path/to/sql"]).then(function (sql) {
    templates = sql;
    return sql;
  });
}

module.exports = function () {
  if (templates) {
    return Promise.resolve(templates);
  } else {
    return init();
  }
}

/model.js

var templates = require("./sql-templates");

// Use those templates
templates().then(function (templates) {
  var sql = templates("/path/to/sql", {
    one: "fish"
  });
  // use sql
});

As files are loaded once on app start it would seem there is no performance reason to load async and seems more cumbersome to use.

Hopefully I've missed some easy way to initialise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions