xjslang/djs

JavaScript with Defers

3

stars

225

commits

Go

primary language

Aug 31, 2026

updated

README

DJS (Defers for JavaScripts)

JS with defer(s) built of top of XJS.

function init() {
  let db = openDb()
  defer closeDb(db)

  let file = openFile('myfile.txt')
  defer {
    console.log('closing file')
    closeFile(file)
  }

  // db and file operations ...
}
Is transpiled to:
function init() {
  let __defers_2a78ec64__ = [];
  try {
    {
      let db = openDb();
      __defers_2a78ec64__.unshift(() => {
        closeDb(db);
      });
      let file = openFile("myfile.txt");
      __defers_2a78ec64__.unshift(() => {
        {
          console.log("closing file");
          closeFile(file);
        }
      });
    }
  } finally {
    for (let defer of __defers_2a78ec64__) {
      try {
        defer();
      } catch (e) {
        console.error(e);
      }
    }
  }
}

Why?

For language ergonomics. Writing a try/finally block for every resource we want to release is cumbersome and hinders the natural reading of the code.

[!NOTE] The latest versions of JS already have the using declaration, but it is still cumbersome and only available recent versions.

Requirements & Install

You'll need Go. Once installed, simply do this:

git clone <this repo>
go install ./cmd/djs

Examples of use:

djs <file.djs>         # compile DJS to JS
djs <file.djs> | node  # compile and run
djs -format <file.djs> # format DJS
djs -check <file.djs>  # check for errors

Contributors

gchumillas

225 commits

xjslang/djs

JavaScript with Defers

3

stars

225

commits

Go

primary language

Aug 31, 2026

updated

README

DJS (Defers for JavaScripts)

JS with defer(s) built of top of XJS.

function init() {
  let db = openDb()
  defer closeDb(db)

  let file = openFile('myfile.txt')
  defer {
    console.log('closing file')
    closeFile(file)
  }

  // db and file operations ...
}
Is transpiled to:
function init() {
  let __defers_2a78ec64__ = [];
  try {
    {
      let db = openDb();
      __defers_2a78ec64__.unshift(() => {
        closeDb(db);
      });
      let file = openFile("myfile.txt");
      __defers_2a78ec64__.unshift(() => {
        {
          console.log("closing file");
          closeFile(file);
        }
      });
    }
  } finally {
    for (let defer of __defers_2a78ec64__) {
      try {
        defer();
      } catch (e) {
        console.error(e);
      }
    }
  }
}

Why?

For language ergonomics. Writing a try/finally block for every resource we want to release is cumbersome and hinders the natural reading of the code.

[!NOTE] The latest versions of JS already have the using declaration, but it is still cumbersome and only available recent versions.

Requirements & Install

You'll need Go. Once installed, simply do this:

git clone <this repo>
go install ./cmd/djs

Examples of use:

djs <file.djs>         # compile DJS to JS
djs <file.djs> | node  # compile and run
djs -format <file.djs> # format DJS
djs -check <file.djs>  # check for errors

See what people are saying

Contributors

gchumillas

225 commits

Languages

Go

97.3%

Dogescript

2.7%