Skip to content

Instantly share code, notes, and snippets.

@aal89

aal89/boot.ts

Last active Jan 29, 2020
Embed
What would you like to do?
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