6

Maybe I'm not debugging promises right but basically if you stop at break point and run async code it doesnt actually finishes until you resume execution and that's a problem. Debugger allows you to quickly experiment with multiple api methods... but you cant if you resume it

debugger;
//now type the following in console
Promise.resolve().then(()=> console.log('done'));
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168

2 Answers2

4

A possible workaround for this is to put debugger in your .then callback as well. This won't work in all situations but it worked for my particular case of debugging node.js scripts before they exit:

  1. insert this into the JS code that you want to debug

    debugger;
    
  2. when the debugger stops, type the following at the console prompt:
    expressionReturningPromise().then( r => {
      console.log('done');
      debugger;
    });
    
  3. resume script execution

The dev tools will then pause on the debugger within the .then callback and you'll have the resolved value of your promise available for examination.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
derpedy-doo
  • 1,097
  • 1
  • 18
  • 22
0

It doesn't execute because the function in .then is only called when the current "thread" is finished. This is the same for all asynchronous calls such as setTimeout.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 2
    yes, so that can be fixed. or is there another way to debug promises based methods – Muhammad Umer Nov 10 '17 at 06:29
  • @MuhammadUmer Methods of debugging promises is another problem. My answer simply points out that the callback in `.then` will not be execute until the current block of code ("thread") is finished. – Derek 朕會功夫 Nov 10 '17 at 07:33
  • 1
    If you're using Chrome / Chromium, you won't be able to run await in the DevTools console due to this issue : https://bugs.chromium.org/p/chromium/issues/detail?id=833928 – Kamagatos Jan 25 '20 at 03:58
  • `console.log(3); await (new Promise((r,x)=> setTimeout(function(){console.log(4); r(); }, 2000))) console.log(5);` – Muhammad Umer Mar 26 '20 at 19:01