Javascript: calling asynchronously-initialized libraries from synchronous code

With the power of await, this isn't so difficult: let nacl, scrypt; function main() { /* Synchronous code that calls nacl and scrypt */ } (async () => { [nacl, scrypt] = await Promise.all([new Promise(nacl_factory.instantiate), new Promise(scrypt_module_factory)]); return main(); })();

Basic JS Async Cookbook

function asleep(delay) { return new Promise(resolve => setTimeout(resolve, delay)); } // Usage: await asleep(1000); function aalert(message) { return new Promise( resolve => resolve(alert(message)) ); } function aconfirm(message) { return new Promise( resolve => resolve(confirm(message)) ); } function aprompt(message, deflt) { return new Promise( resolve => resolve(prompt(message, deflt)) ); } // Usage: await aalert("The task will begin as soon as you press OK.") ; do_task(); aalert("The task will begin as …