Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 405
Muhammad Umer Avatar asked Mar 03 '18 08:03

Muhammad Umer


People also ask

How do I resolve a Promise in node JS?

The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.

What are the advantages of using promises instead of callbacks?

They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.

Does Promise all run in parallel JavaScript?

As you can see, Promise. all executes code concurrently, but what is parallel execution? JavaScript is single-threaded and can only perform a single chunk of work at a time, so parallel execution is not possible with JavaScript, except for some circumstances such as web workers.


1 Answers

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);
like image 134
Binier Avatar answered Oct 19 '22 06:10

Binier