Urgent.News

What's breaking now, across thousands of outlets.

Tech

Why console.log Shows 'Promise {<pending>}' Instead of Your Actual Value

That's not a broken await — it's JavaScript reporting exactly what the promise's state was the instant you logged it. Here's why the value isn't there yet, and how to actually wait for it. Adapted from the JavaScript Essentials Companion Guide . You write a function to fetch a user, call it, and log the result to check it worked: async function getUser ( id ) { return fetch ( `/api/users/ ${ id }…

The `console.log` statement in JavaScript does not display the actual value of a promise; instead, it shows the promise itself wrapped in `Promise { pending }`. This happens because the promise is not yet resolved at the moment the log is executed. The issue arises when an async function is called and its result is logged immediately, before the promise has had a chance to settle.

The console.log function retrieves the current state of the variable, which in this case is the pending promise. To resolve this, you need to use the `await` keyword inside another async function to pause execution until the promise is resolved, allowing you to access the actual value. Alternatively, you can use the `.then()` method to wait for the promise to resolve and then log the resolved value.

Understanding the difference between an async function returning a promise and the actual value contained within that promise is crucial to avoid confusion.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Our test clicked a button our users couldn't reach

We shipped a small blog editor inside our dashboard. The same evening I opened it and the form was cut off at the bottom of the screen. The body field was half visible.

  • New blog editor added with bug
  • Form taller than screen causing clipping
  • Test assumed scrollable form, not fixed

Laramod: Laravel modules without the magic that still feels magical

Why Every Laravel application starts out tidy. Then it grows, and one day app/Http/Controllers has eighty files in it, billing sits beside the blog, the blog sits beside the support desk, and the only…

  • Laramod simplifies Laravel modular applications without magic
  • Modules defined by contracts, not file system or composer files
  • One file, one module principle for discoverable, easy-to-understand modules

More from Saturday 19 September →