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.

App hang when using promises/async on connecting

See original GitHub issue

I’m using NodeJS 8.11.3 with Monk 6.0.6 and I have the next piece of code:

function monkConnect(url) {
    return new Promise(resolve => {
        resolve(monk(url));
    });
}

await monkConnect("some valid url");

I have tried in so many ways to make this works( using the callback from monk(url), making the function async and calling await on monk, and so many methods) and the code just freeze/hang and nothing happen and I don’t know how to resolve it. Please some help.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

4reactions
Darkhoggcommented, Nov 20, 2019

A year later, this is still a problem, and I don’t think there’s a solution that doesn’t break compatibility.

The only workaround I’ve been able to use is:

function connect (uri) {
    const db = monk(uri);
    db.then = null;

    return new Promise((accept, reject) => {
      db.once('open', () => accept(db));
      db.once('error-opening', reject);
    });
}

Essentially, remove the then to avoid JS treating it as a then-able (which will cause it to never resolve, because it resolves to itself in a loop forever), then constructing my own promise.

1reaction
szm1commented, Aug 7, 2018

I ran into this problem yesterday as well and have tried all manner of things. I’m not sure if the monk constructor is returning a proper promise. It sets up a bunch of functions on it including a then function:

Manager.prototype.then = function (fn) {
  return new Promise(function (resolve, reject) {
    this.once('open', resolve)
    this.once('error-opening', reject)
  }.bind(this)).then(fn.bind(null, this))
}

However, if mongo isn’t actually running when monk tries to connect, it doesn’t use the reject function passed into the then function above, it just breaks out and I get an unhandled promise rejection error.

It would be great if this problem could get fixed, because I’m trying to use async/await to wait until monk connects before trying to get some data out of the database, but that’s not possible currently.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Promise.all to Stop Async/Await from Blocking Execution ...
Use Promise.all to Stop Async/Await from Blocking Execution in JS. When writing asynchronous code, async/await is a powerful tool — but it comes...
Read more >
usage of promise with while loop results in freezing ui
When I attempt to execute new version of the code using promise browsers freezes and seems to be executing infinite loop. Could you...
Read more >
Breaking Ui Code Into Async Blocks And Avoid Freezing Ui
This is a technical article on how Acousterr's tab parser utilizes Javascript Promises to avoid freezing UI while parsing is in progress. Background...
Read more >
How to use promises - Learn web development | MDN
With a promise-based API, the asynchronous function starts the operation and returns a Promise object. You can then attach handlers to this ...
Read more >
Changing Async/Await to Promises.allSettled() to Speed Up ...
And since it iterates over the records and updates them one by one, the time goes up linearly as there are more records...
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