This Gist serve as an illustration for a Stack Overflow thread. It contains a real situation, where a Bluebird promise needs to receive its handler asynchronously.
The goal is to launch a long process, which writes a preview to stdout upon startup. The launchProcess()
function should launch the process and return once the preview has been read. In particular, it should not wait for the completion of the process.
As an example, you would could think to a large file download (written to the disk), with the HTTP headers written to stdout as soon as they are received.
launchProcess.js
: the initial and intuitive implementation, which triggers a Bluebird warning about a possible unhandled rejection (false positive).launchProcess_workaround.js
: the same as above, but with a workaround to avoid the false positive. Although more compact, this is also less intuitive in my opinion.client.js
: a possible quick & dirty client to illustrate howlaunchProcess()
could be called.
You have a race condition based on whether or not completion is already rejected/resolved when you check for
isRejected
, if it rejects after startup completed its rejection will be suppressed if I'm reading this correctly.