Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid forgotten promise returns

When I'm using promises to express dependencies between jobs, where the resolved value becomes unimportant, there is some danger that I might be forgetting a return somewhere. Example:

startSomething().then(function() {
  Q.all(tasks.map(function(task) { return task.startIt(); }))
}).then(collectOutput).done();

Here the Q.all returns a promise, and I should have returned that. Not doing so means that by the time collectOutput gets called, all tasks have been started, but there are no guarantees that they finished.

This kind of error results in a race condition, and may be extremely hard to reproduce and track down. So I wonder, is there some tool to help detect and avoid this kind of problem? Perhaps some promise library which warns when a function along the way returns undefined? Or detects promises with no listeners, the way Bluebird does for unhandled rejections?

like image 773
MvG Avatar asked Dec 21 '15 22:12

MvG


1 Answers

Actually, bluebird will warn you if you created a promise in a handler but did not return it. If you're willing to drop Q.

Here's a more in-depth explanation about bluebird's warnings

Warning: a promise was created in a handler but none were returned from it This usually means that you simply forgot a return statement

somewhere which will cause a runaway promise that is not connected to any promise chain.

For example:

getUser().then(function(user) {
    getUserData(user);
}).then(function(userData) {
    // userData is undefined
});
like image 186
Madara's Ghost Avatar answered Sep 24 '22 13:09

Madara's Ghost