question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[browser-logs] print promises

See original GitHub issue

Promises logged to the console show up as {}.

We can improve that, at the least we can print Promise { } . If possible it would be great to mirror the behavior of the browser/node terminal to also log the Promise status and/or result. I’m not sure if it’s possible to get this status synchronously from the promise object.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
LarsDenBakkercommented, Sep 25, 2020

@43081j thanks for the explanation, it’s spot on.

I think for the first version we can start with a compromise - just return "Promise { }". I don’t know of any way to synchronously check if a promise is already resolved… maybe someone knows a trick but we can also add this later. The most important part is communicating that it actually is a promise so that people can know how to proceed with debugging.

1reaction
43081jcommented, Sep 25, 2020

@ashikvarma11 if you want to pick this up here’s my suggested solution:

The browser-logs code really seems to exist so it can copy a logged value (via console.log) from the browser session to the test runner:

  • Catch all logs
  • Serialise each logged value to a string (using serialize)
  • Pass the string back to the test runner (through playwright/puppeteer or whatever is used)
  • Deserialise the string back to a “fake” object which represents the original one (using deserialize)

So if you want to print promises nicely, you need to change the serialiser to return something like:

{ [KEY_WTR_TYPE]: 'Promise' }

Just like undefined is handled here.

Unfortunately im not aware of a way to check if a promise has resolved or rejected without actually asynchronously catching/then-ing it (which we can’t do here because a JSON reviver is synchronous).

Then in the deserialiser you need to add a case to set a fake promise (or something representing one). Just like the one for functions here:

case 'Promise':
  return new Promise((res) => { /* implementation hidden */ });

it’s not great to be generating loads of unresolved promises i suppose. You could also return a Promise.resolve() but that may be misleading if what was originally serialised was a rejected promise.

Maybe @LarsDenBakker would be happy with some compromise where we don’t make a real promise but just log a string representing one instead, i’m not sure.

Read more comments on GitHub >

github_iconTop Results From Across the Web

console logging prints a promise when function returns an ...
In the first way: const get = async (endpoint: string): Promise<object> => { const response: Response = await fetch(`${process.env.
Read more >
Promise.resolve() - JavaScript - MDN Web Docs
The Promise.resolve() method "resolves" a given value to a Promise. If the value is a promise, that promise is returned; if the value...
Read more >
Console logs inside Promise functions not working ... - GitHub
I'm on Ubuntu 14.04LTS. Using Google Chrome (38.0.2125.111 (64-bit)) & Firefox (33.0). I'm using the pouchdb.min.js (From commit e78a30f on ...
Read more >
Javascript: How to access the return value of a Promise object
It's happening because the Javascript code always executes synchronously, so the console.log() function starts immediately after the fetch() ...
Read more >
How to Make your Console Output Fun and Interactive in ...
We'll add it by explicitly printing the \n character. const log = (s) => { for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found