question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lot of promises remain unresolved

See original GitHub issue

I’m using deferred.monitor() to monitor all the promises that are still unresolved after 20 seconds the end of the program

This is the code:

deferred.monitor();
deferred.monitor(20000, function (err) {
    console.log('PROMISE',err.stack)
});

The problem is that a lot of promises remain unresolved, but they shoudn’t (the line def.resolve is executed).

Someone of them rejects an error, but in this case they should be considered as resolved, shoudn’t they?

May be there some causes of this behaviour?

PS: hoping this is the reight place where I can talk about this topic

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
medikoocommented, Jan 19, 2018

@anjil all is as expected, and there’s no issue. See it’s “Initialized as Unresolved”, not “Never resolved”. It’s those that never resolve that signal an issue.

All promises created with deferred() and then resolved with either deferred.resolve and deferred.reject are understood as “Unresolved at initialization”.

To have promise resolved at initialization, you would have to create resolved promise right away via e.g. deferred('foo'), which will create promise that’s already resolved with foo value.

0reactions
anjilcommented, Jan 19, 2018

I used the same code shared in the documentation and even that give 2 unresolved promises. The sample is as below:

var deferred = require('deferred');

deferred.profile();

var delay = function (fn, timeout) {
  return function () {
    var def = deferred(), self = this, args = arguments;
 
    setTimeout(function () {
      var value;
      try {
        value = fn.apply(self, args);
      } catch (e) {
        def.reject(e);
        return;
      }
      def.resolve(value);
    }, timeout);
 
    return def.promise;
  };
};
 
var delayedAdd = delay(function (a, b) {
  return a + b;
}, 100);
 
var resultPromise = delayedAdd(2, 3);
 
console.log(deferred.isPromise(resultPromise)); // true
 
resultPromise(function (value) {
  // Invoked after 100 milliseconds
  console.log(value); // 5
});

var stats = deferred.profileEnd(); // End profiling
console.log(stats.log);

Output is:

true
------------------------------------------------------------
Deferred usage statistics:

2 Total promises initialized
2 Initialized as Unresolved
0 Initialized as Resolved

Unresolved promises were initialized at:
1 at /home/node/Documents/learn/scrapping/index.js:7:15
1 at Object.<anonymous> (/home/node/Documents/learn/scrapping/index.js:32:1)
------------------------------------------------------------

5

Please let me know what’s the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Finding unresolved promises in JavaScript | Swizec Teller
Finding unresolved promises is an instance of the halting problem – a provably unsolvable problem. There is no algorithm you can write that...
Read more >
Process exits with unresolved promises · Issue #16 - GitHub
A lot of users struggle understanding why a Node.js process exits while a promise is still running not yet resolved (see e.g., ...
Read more >
Creating a (ES6) promise without starting to resolve it
Good question! The resolver passed to the promise constructor intentionally runs synchronous in order to support this use case:
Read more >
Debug Tips - finding an unresolved promise using console.log
Sometimes as a beginner you might code a while loop or a for loop and accidentally create an infinite loop, or maybe you...
Read more >
How can I check if there are any unresolved promises ... - Reddit
But if I happen to have accidentally have an outstanding promise still running at the end of my test, that is not detected....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found