Avoid Forgotten Promises

When I use promises to express dependencies between jobs where the resolved value becomes irrelevant, there is a certain danger that I might forget about the return somewhere. Example:

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

Here Q.all returns the promise, and I had to return it. Not doing this means that by the time collectOutput all tasks were started, but there was no guarantee that they had finished.

Such an error leads to a race condition, and it is very difficult to reproduce and track. Therefore, I am wondering if there is any tool that will help to detect and avoid such a problem? Perhaps some kind of promise library that warns when a function returns undefined along the way? Or discovers promises without listeners, like Bluebird does for raw failures?

+6
source share
1 answer

Actually, bluebird will warn you if you created a promise in the handler, but did not return it, if you are ready to refuse Q.

Here's a more detailed explanation of bluebird warnings

Warning: a promise was created in the handler, but was not returned from it. This usually means that you just forgot the return statement.

somewhere that will cause an unbridled promise that is not related to any chain of promises.

For instance:

 getUser().then(function(user) { getUserData(user); }).then(function(userData) { // userData is undefined }); 
+2
source

All Articles