Reputation: 18097
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
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:
Unfortunately at the moment best and simplest ways I am aware of are:
db.User.create().then(x => global.a = x);
// use global.a as you'd like:
console.log(global.a);
Upvotes: 3
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
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
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
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