Muhammad Umer
Muhammad Umer

Reputation: 18097

Is there a way to evaluate promise in nodejs without resuming debugger?

If I type debugger and I want to check something. But call to that function returns a promise, then i am stuck.

For example:

I typed debugger and it stopped.

function test(db) {
    debugger;
    // here i want to see something
    var a = .....;
}

But if I type

let d = db.User.create(); 

I'll get

Promise { pending }

now there is no recourse. I can't simply evaluate promise. Kinda make whole debugger less useful.

This would have been no problem, if it was synchronous, I'd have been able to jump in mid program, check out few things and modify program to my liking then run rest of the program.

Upvotes: 21

Views: 3464

Answers (5)

Binier
Binier

Reputation: 1107

Probably in order to understand why promises can't be resolved while debugger is paused, you need to understand how Event Loop works. There are plenty of resources about it so no need to explain it here.

When you pause a Debugger, Event Loop gets paused too. And everything asynchronous is scheduled and handled in Event Loop.

Possible ways to resolve promise in debugger:

  • Somehow execute promise synchronously by pooling it manually. But as far as I checked, there is no way to do that. V8 doesn't give us that level of control. But maybe it can be achieved by writing Native Addon. Perhaps a function that accepts a promise and resolves it using V8's internals synchronously.
  • Create a new Web Worker or another process and execute the promise there. But you'd have to serialize the data and possibly recreate the state. Though it won't work anyways since creating and passing messages to new "thread" is asynchronous, so...

Unfortunately at the moment best and simplest ways I am aware of are:

  1. Print current state and use it to construct and execute code outside of the debugger.
  2. Assign result to a variable and continue(F5) debugger, so that your promise can be resolved. After use it as you'd like:
db.User.create().then(x => global.a = x);
// use global.a as you'd like:
console.log(global.a);

Upvotes: 3

Renaud Reguieg
Renaud Reguieg

Reputation: 81

const foo = async(db)=>{//just be sure that you are in async for this one

try{

let d = await db.User.create(); 
//now d is the result
}catch (err) {console.log(err)}

Or more basically

 db.User.create().then((data)=>console.log('user created',data))
                 .catch(err=>console.log('err'));

That's how you resolve promises. Don't forget to add the catch or you will have some trouble.

Upvotes: 0

Prashant Biradar
Prashant Biradar

Reputation: 323

Please refer https://gist.github.com/martinheidegger/4c2b7908005f65f9a53b25d5fec65f63 check debug-promise.js module

'use strict'

var debug = require('./debug-promise')

debug.wrap(() => new Promise(() => {})).then(() => console.log('a executed'))
debug.wrap(() => Promise.resolve('x')).then(() => console.log('b executed'))


//
// This will output something like:
//
//   b executed
//   1 Promise is still unfullfilled!
//   --- (V347P-M6K) ---
//     at Object.<anonymous> (/debug-test2.js:5:7)
//     at Module._compile (module.js:571:32)
//     at Object.Module._extensions..js (module.js:580:10)
//   
//     CODE
//       () => new Promise(() => {})
//   
//   ---
//

Upvotes: 0

Sohail
Sohail

Reputation: 4586

Chrome not too long ago launched async debugging in their Chrome Devtools. You need to enable the flag in chrome to use it for now.

Check out this nice article here - https://www.html5rocks.com/en/tutorials/developertools/async-call-stack/

Upvotes: 0

Charlie
Charlie

Reputation: 23778

Use the debugger at the either event of the promise being resolved or rejected.

function test(db) {

   let d = db.User.create(); 

   d.then((result) => {
      debugger;
      ...
   },
   (e) => {
      debugger;
      ...
   })


}

Upvotes: 0

Related Questions