I ran into an issue where I forgot to resolve my promise, leaving the remainder of the chain waiting (Forever). Thankfully in my case I was able to track down the issue in only 10 or so minutes, but I could see this being a really big pain in the butt if I don't figure out some way to check for orphaned promise chains.

How would you automate checking for the following error?

function myAsyncFunction(){
return new Promise((resolve,reject)=>{
  //do stuff here, but forget to call resolve();
  • 1
    The promise engine has no way of knowing if a promise is supposed to be resolved in the next few ms or not for hours or days or even if only in some circumstances. I could imagine a diagnostic tool that might keep track of all unresolved promises in some sort of weakMap and let you query that, but I don't see how anyone can know what promise is open on purpose vs. accidentally left open. – jfriend00 Jan 30 '17 at 1:20
  • 1
    Not a javascript person, but looking for an extensible linter and putting in this rule might be reasonable. It seems ESLint has features in this direction. – walpen Jan 30 '17 at 3:16
  • thanks, I think I will end up wrapping all my new Promise() object creations in a derived class that, at devTime, tests for long-running chains (using Promise.timeout) because at least in my situation promises that take longer than 30 seconds is bad. – JasonS Jan 31 '17 at 18:02

Most people do the check in a regular unit test. You can't test a promise without testing that it resolves and giving it a timeout appropriate to the unit under test.

| improve this answer | |

Async tests should have done() callback. It's the responsibility of the test writer to call it. It helps, of course to set default reasonable timeout, but beware slow continuous integration servers that might exceed too optimistic expectations based on your 8-core latest workstation :)

Then, if you forget to return any promise in the chaining, you will get null value (you should check return values in tests, right). If you forget the done, you will get timeout exception and a failed test.

Check out eg. jasmine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.