10

If I drop into the VSCode debugger in some javascript code and call an asynchronous function with await it just returns a promise. How can I resolve the promise within the debugger so I can see what the result is?

For example, if I define a function like so:

const doAsyncThing = async () => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(5)
        }, 1000)
    })
}

(async function() {
    const result = await doAsyncThing()
    console.log(`result is ${result}`)
    debugger
})()

Then this happens when I call it in the debugger:

result is 5
> result
5
> await doAsyncThing()
Promise {<pending>}
> const result2 = await doAsyncThing()
Promise {<pending>}
> result2
Uncaught ReferenceError: result2 is not defined
> doAsyncThing().then(r => console.log(r))
Promise {<pending>}
> await doAsyncThing().then(r => console.log(r))
Promise {<pending>}

How can I make it resolve the promise and get me the result within the debugger?

This is not a duplicate of How to debug async/await in visual studio code? That question appears to be asking how to place a breakpoint within an asynchronous function, while I am trying to actually execute the function from the debugger. I previously asked this question here but it was closed (incorrectly I believe) as a duplicate. I added additional context here to help clarify the difference.

Alex Long
  • 261
  • 2
  • 5
  • instead of `const result = await doAsyncThing()` you could try `doAsyncThing().then(result => ...)` – TKoL Dec 01 '20 at 17:30
  • i'm not sure what to do in the `...` section though, apart from console.logging something, or maybe assigning the result to a global variable – TKoL Dec 01 '20 at 17:30
  • 1
    also, if you've hit a breakpoint and you're trying to get access to `result` while in a breakpoint... i'm not actually sure that's even possible really – TKoL Dec 01 '20 at 17:31
  • @TKoL Thanks, I did exactly what you suggested in the code example above and used console.log but it still did not log the result. – Alex Long Dec 01 '20 at 17:37

1 Answers1

1

When you using debugger, it also pauses app.

Best way to know result of function is doAsyncThing().then(console.log), but function will not be executed until you press F5 key (Continue).

While app is not paused, you can use debug console to call async functions and this will work. Best way to check this is next code:

const doAsyncThing = async () => {
  return new Promise((resolve) => {
      setTimeout(() => {
          resolve(5)
      }, 1000)
  })
}

global.testFn = doAsyncThing;

new Promise((resolve, reject) => {
  setTimeout(resolve, 5000)
});

And use next command in debug console: global.testFn().then(console.log)

Result

Note, that I'm using global, because debug console don't have context of file until you paused app. But you can use modules by require:

var mongoose = require('mongoose');
var model = mongoose.model('Users');
model.findOne().then(console.log).catch(console.warn);
Drom Hour
  • 94
  • 4