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.
@bergus, if
completion
fails beforestartup
settles, then the rejection gets correctly transferred bycheckError
. But yes, the workaround is better here because the resulting promise is immediately rejected, without waiting for the settling ofstartup
.