Skip to content

Instantly share code, notes, and snippets.

@neharkarvishal
Last active March 19, 2021 06:30
Show Gist options
  • Save neharkarvishal/5783499b06c13a6fcf648f66465148d1 to your computer and use it in GitHub Desktop.
Save neharkarvishal/5783499b06c13a6fcf648f66465148d1 to your computer and use it in GitHub Desktop.
/**
* 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