Skip to content

Instantly share code, notes, and snippets.

@aal89
Last active January 29, 2020 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aal89/d2460b99d9b7857137a1317eea17c9fe to your computer and use it in GitHub Desktop.
Save aal89/d2460b99d9b7857137a1317eea17c9fe to your computer and use it in GitHub Desktop.
Bootstrapper (chassis pattern) with a restarting capabilities for TypeScript/Javascript using 'top-level' async/await.
// random bootstrapper for any theoretical ts/js application
const timeout = (millis: number, fn: () => void) => new Promise(c => setTimeout(c, millis)).then(fn);
(async function boot() {
// this try-catch is an additional safety net used for (poorly) written applications in which errors
// are not properly caught
try {
// some random loading of initial components
const config = {}; // some fs load config from disc
// call some main function once done booting and inject hypothetical config
require('main.js').main(config)
} catch (err) {
console.error(err);
// timeout magic
console.info('I seem to be crashed, rebooting...');
// await a timeout before rebooting to make sure we dont spam logs, let the environment 'settle', etc...
await timeout(10000, boot);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment