Home > Web Front-end > JS Tutorial > Why Does My Asynchronous Function Return `Promise { }` Instead of a Value?

Why Does My Asynchronous Function Return `Promise { }` Instead of a Value?

Barbara Streisand
Release: 2024-12-13 06:07:17
Original
833 people have browsed it

Why Does My Asynchronous Function Return `Promise {  }` Instead of a Value?

Asynchronous Function Returning Promise { } Instead of a Value

As demonstrated in the provided code, asynchronous functions often return a Promise, which represents the eventually available value. However, when attempting to access its value immediately, the console logs "Promise { }" due to the unresolved state of the Promise.

Resolving Promise Values

To obtain the desired token value, you must chain a ".then" handler to your promise. This handler captures the resolved result, whether it's already available or still pending:

let AuthUser = (data) => {
  return google.login(data.username, data.password).then((token) => {
    return token;
  });
};

let userToken = AuthUser(data);
userToken.then((result) => {
  console.log(result); // "Some User Token"
});
Copy after login

Promise Resolution Details

Promises adhere to the Promises/A specification, which mandates a specific resolution procedure:

  • If the ".then" handler returns a value, the Promise resolves to that value.
  • If the handler returns another Promise, the Promise resolves to the resolved value of the chained Promise.

Understanding Promise Chaining

Each ".then" handler's resolved value becomes the next handler's input, enabling a chain of asynchronous operations. The following examples illustrate this behavior:

Value Return:

function initPromise() {
  return new Promise((res, rej) => {
    res("initResolve");
  });
}

initPromise()
  .then((result) => {
    console.log(result); // "initResolve"
    return "normalReturn";
  })
  .then((result) => {
    console.log(result); // "normalReturn"
  });
Copy after login

Chained Promise Return:

function initPromise() {
  return new Promise((res, rej) => {
    res("initResolve");
  });
}

initPromise()
  .then((result) => {
    console.log(result); // "initResolve"
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve("secondPromise");
      }, 1000);
    });
  })
  .then((result) => {
    console.log(result); // "secondPromise"
  });
Copy after login

The above is the detailed content of Why Does My Asynchronous Function Return `Promise { }` Instead of a Value?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template