Last active
March 19, 2021 06:30
-
-
Save neharkarvishal/5783499b06c13a6fcf648f66465148d1 to your computer and use it in GitHub Desktop.
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
/** | |
* Workaround to set up a handshake, lazy-loading by resolving a promise | |
*/ | |
let settleReadyState = {} | |
let subprocess = {} | |
// following code outputs: Promise {<pending>} on console and sets | |
// settleReadyState = { resolve: ƒ, reject: ƒ} | |
// subprocess = {ready: Promise<pending>} | |
subprocess.ready = new Promise( | |
(resolve, reject) => Object.assign(settleReadyState, { resolve, reject }) | |
) | |
// following code outputs: subprocess = {ready: Promise<fulfilled>} | |
subprocess.once('message', () => { | |
settleReadyState.resolve() | |
subprocess.on('message', console.log); | |
}) | |
// lazy-loading and sending message | |
subprocess.ready.then( | |
() => subprocess.send({ hello: 'child' })} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment