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,
.race()
is very nice. I come fromQ
, and I didn't noticed it yet. Thanks!However, if
completion
gets resolver beforestartup
(for some reasons), the wrong result may be resolved. I guess that.race()
is primarily intended for homogeneous values.Also, I don't get your point about the cancellation of
completion
. I cannot find any information about this in the API reference.Regarding your suggestion for moving the error handler, it was also my first intention but it would mix semantically unrelated code.