Thanks for another fun challenge!
This took quite a bit of thinking, but I ended up working with the fact that setTimeout isn't actual asynchronous. So here's what my log function looks like:
setTimeout
log
function log(text) { let logger = { label: "Log:", message: [ text ], withData(...data) { this.label = "withData:"; this.message.push(...data); } }; setTimeout(() => console.log(logger.label, ...logger.message), 0); return logger; }
And it passes the tests, of course!
log("test"); // > log: test log("test").withData(2); // > withData: test 2
It can even handle more complicated synchronous behavior!
let logger = log("Here's a"); if (Math.random() > 0.5) { logger.withData("number", Math.random()); } else { logger.withData("boolean", Math.random() > 0.5); }
I'm glad you enjoyed it :) cool solution. More extensible than mine for sure.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for another fun challenge!
This took quite a bit of thinking, but I ended up working with the fact that
setTimeoutisn't actual asynchronous.So here's what my
logfunction looks like:And it passes the tests, of course!
It can even handle more complicated synchronous behavior!
I'm glad you enjoyed it :) cool solution. More extensible than mine for sure.