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

查看:3104
本文介绍了为什么我的异步函数返回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)获取令牌,该令牌将一个Promise返回到变量中.然后才执行一些操作.

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将始终记录为未决.无论承诺状态(已解决还是仍处于待处理状态),您都必须在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"
})

那是为什么?

承诺只是向前的方向;您只能解决一次. 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是一个可转换的,它将尝试使承诺采用以下状态 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.

这种对罐头的处理使得允诺实现能够 互操作,只要它们暴露了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,则已链接承诺的解析值将传递到以下.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天全站免登陆