I'm attempting to display the console.log
output in a div. I'm doing this by overriding the console.log
function
When I use the original console.log
, a promise displays, in the console as (see 1. in code below):
Promise { <state>: "fulfilled", <value>: undefined }
When I over-ride it I get, in the div: :
[object Promise]
(see 2. in code below)
How would you adjust the following code to have the <state>
property displayed in the "main" div?
const pause = sec => new Promise(r => setTimeout(r, 1000 * sec))
// 1. Original console.log
;(async function(){
await new Promise(resolve => document.addEventListener('DOMContentLoaded', resolve));
let x = pause(0.5)
console.log(x)
await x;
console.log(x)
})();
// 2. Overridden console.log
;(async function(){
await new Promise(resolve => document.addEventListener('DOMContentLoaded', resolve));
await pause(1)
let divconsole = document.getElementById('main');
// Override console.log:
console.log = function () {
for (let i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'object') {
divconsole.innerHTML += 'console.log arg ' + String(i) + ': ' + String(arguments[i]) + '<br>';
}
}
}
let y = pause(0.5)
console.log(y)
await y;
console.log(y)
})();
<div id="main" style="width:100%;height:auto;background-color:lightblue;">
</div>
A note on why I'm asking: I have code with a promise that resolves in a desktop browser and not in a mobile browser. I'm attempt to debug this (without consistent mobile-desktop OS ecosystem). I might write a follow up question if I can replicate it in a simple example.