为什么我的异步函数返回 Promise { <pending>而不是一个值? [英] Why is my asynchronous function returning Promise { <pending> } instead of a value?

查看:42
本文介绍了为什么我的异步函数返回 Promise { <pending>而不是一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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

当我尝试运行这样的东西时:

And when i try to run something like this:

let userToken = AuthUser(data)
console.log(userToken)

我得到:

Promise { <pending> }

为什么?

我的主要目标是从 google.login(data.username, data.password) 获取令牌,它返回一个承诺,放入一个变量中.然后才执行一些操作.

My main goal is to get token from google.login(data.username, data.password) which returns a promise, into a variable. And only then preform some actions.

推荐答案

只要结果尚未解决,promise 将始终记录挂起.您必须在承诺上调用 .then 以捕获结果,而不管承诺状态如何(已解决或仍在等待):

The promise will always log pending as long as its results are not resolved yet. You must call .then on the promise to capture the results regardless of the promise state (resolved or still pending):

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

let userToken = AuthUser(data)
console.log(userToken) // Promise { <pending> }

userToken.then(function(result) {
   console.log(result) // "Some User token"
})

这是为什么?

Promises 只是向前的;您只能解决一次.Promise 的解析值被传递给它的 .then.catch 方法.

Promises are forward direction only; You can only resolve them once. The resolved value of a Promise is passed to its .then or .catch methods.

根据 Promises/A+ 规范:

According to the Promises/A+ spec:

promise 解析过程是一个抽象操作,取为输入一个承诺和一个值,我们将其表示为 [[Resolve]](promise,X).如果 x 是一个 thenable,它会尝试让 promise 采用以下状态x,假设 x 的行为至少有点像 a承诺.否则,它以 x 值履行承诺.

The promise resolution procedure is an abstract operation taking as input a promise and a value, which we denote as [[Resolve]](promise, x). If x is a thenable, it attempts to make promise adopt the state of x, under the assumption that x behaves at least somewhat like a promise. Otherwise, it fulfills promise with the value x.

这种对 thenables 的处理允许 promise 实现互操作,只要它们公开 Promises/A+ 兼容然后方法.它还允许 Promises/A+ 实现同化"使用合理 then 方法的不一致实现.

This treatment of thenables allows promise implementations to interoperate, as long as they expose a Promises/A+-compliant then method. It also allows Promises/A+ implementations to "assimilate" nonconformant implementations with reasonable then methods.

这个规范有点难以解析,所以让我们分解一下.规则是:

This spec is a little hard to parse, so let's break it down. The rule is:

如果 .then 处理程序中的函数返回一个值,则 Promise 将使用该值进行解析.如果处理程序返回另一个 Promise,则原始 Promise 将使用链接的 Promise 的已解析值进行解析.下一个 .then 处理程序将始终包含在前面的 .then 中返回的链式承诺的解析值.

If the function in the .then handler returns a value, then the Promise resolves with that value. If the handler returns another Promise, then the original Promise resolves with the resolved value of the chained Promise. The next .then handler will always contain the resolved value of the chained promise returned in the preceding .then.

下面更详细地描述了它的实际工作方式:

The way it actually works is described below in more detail:

1..then 函数的返回值将是 promise 的解析值.

1. The return of the .then function will be the resolved value of the promise.

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

initPromise()
  .then(function(result) {
    console.log(result); // "initResolve"
    return "normalReturn";
  })
  .then(function(result) {
    console.log(result); // "normalReturn"
  });

2.如果 .then 函数返回一个 Promise,那么该链式 Promise 的解析值将传递给下面的 .then.

2. If the .then function returns a Promise, then the resolved value of that chained promise is passed to the following .then.

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

initPromise()
  .then(function(result) {
    console.log(result); // "initResolve"
    return new Promise(function(resolve, reject) {
       setTimeout(function() {
          resolve("secondPromise");
       }, 1000)
    })
  })
  .then(function(result) {
    console.log(result); // "secondPromise"
  });

这篇关于为什么我的异步函数返回 Promise { &lt;pending&gt;而不是一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆