Atlas Functions: > pending promise returned that will never resolve/reject

Hi everyone! I faced with some trouble in Atlas function which I totally can’t debug and even normally google it. The case in I put all my business logic into NPM package and publish it, then I’m importing it and using in function. In code below doMagic just an Async Function which do some stuff with collections, but when I’m running function I’m getting pending promise returned that will never resolve/reject as a result. Any clues what’s wrong or in which direction should I dig?

exports = async function () {
  const { doMagic } = require('@my/dependency');
  
  const mongodb = context.services.get("xxx")
  const db = mongodb.db("xxx")
    
  const result = await doMagic(db);
  return result
};

In more details: doMagic is an async function which starts a bunch of js-generators and executes them step by step, simultaneously collecting all results, awaits until they all will stops. Then returns results map. Nothing very extraordinary, but a lot of algo-stuff. I don’t wanna copy-paste all this stuff into function body in UI to debug the issue step-by-step, I’d like to know what pending promise returned that will never resolve/reject mean at all?

Hi @Trdat_Mkrtchyan welcome to the community!

The function you posted looks pretty straightforward, so I doubt the message was caused by that function. Rather, it’s likely was bubbled up from the doMagic function. Presumably this error was thrown from the await doMagic() line there. However without access to the actual doMagic function, my best educated guess follows :slight_smile:

Just taking the error message at face value: pending promise returned that will never resolve/reject I think it’s trying to tell you that you have some Promise that was never resolved, and the Atlas function was awaiting on it.

As a short experiment, could you perhaps load a simplified doMagic function (e.g. just echo the parameters) into the Atlas function, and see if the error still persists? If the error disappears, then perhaps you can add more complexity into the function to see at what point you’re not resolving/rejecting a Promise.

Best regards
Kevin

Hi @kevinadi

Thanks for response. I found the cause of problem. After more detailed debug I found out that inside of doMagic I used callbacks notation for retrieving data from DB, such as:

new Promise((resolve, reject) => collection.find({}, (err, results) => {...}));

Which was the origin of error. I guess mongo driver in function’s context doesn’t allow to do that. When I replaced it with const res = await collection.find({}), the error disappeared.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.