Last active
January 29, 2020 09:54
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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