0

What does this code do?

await new Promise((resolve) => (Bugsnag.notify(ex, eventCallback), resolve));

function eventCallback(event) {
    addMetadata(event, md);
    addMetadata(event, ex.bugsnagMetadata);
}

function addMetadata(event, md) {
    if(!md) return;
    for (let mdKey in md) {
        let mdValue = md[mdKey];
        if(typeof(mdValue) === 'object')
            event.addMetadata(mdKey, mdValue);
        else
            event.addMetadata(mdKey, mdKey, mdValue);
    }
}

Feel free to ignore eventCallback, addMetadata. I've included them for extra context.

I originally asked Bugsnag how to use async/await on Bugsnag.notify and they responded with the above first line of code.

The code works great most of the time, but when I'm debugging a test within mocha, it hangs indefinitely on the await.

How does the resolve eventually get called and in the correct order--after notify has asynchronously completed?

Brandon
  • 695
  • 10
  • 29
  • 2
    "it hangs indefinitely on the await" - would be my expectation aswell. I don't see `resolve` ever being called. However, since it's async, other code may still run. Just nothing in the async function after that `await`. – ASDFGerte Apr 29 '20 at 21:39
  • 1
    I’m voting to close this question because it is based on a false premise: resolve *doesn't* get called. – Quentin Apr 29 '20 at 21:43
  • Related: https://stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises – Quentin Apr 29 '20 at 21:43
  • @Quentin I'm not sure how it is not getting called. It works most of the time. It's in a special case where it appears to hang. I mean, I've been using that code for a few days and it rarely doesn't return from the await. – Brandon Apr 30 '20 at 01:32
  • @ASDFGerte It doesn't hang in most of the cases. I thought await would wait until the async piece was completed (either resolved or rejected). How would it ever be able to return from the function if resolve was never getting called? – Brandon Apr 30 '20 at 01:33

0 Answers0