承诺待定 [英] Promise pending

查看:159
本文介绍了承诺待定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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.

推荐答案

只要尚未解决结果,承诺将始终记录暂挂。无论承诺状态(已解决或仍在等待),您必须在承诺上调用 .then 来捕获结果:

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

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) //will log results.
})

为什么会这样?

承诺只是前进方向,你只能解决它们一次,你只能将他们的价值传递给他们的 .then .catch 方法

Promises are forward direction only, you can only resolve them once, and you can only get their value passed to their .then or .catch methods

根据Promises / A +规范:

According to the Promises/A+ spec:


承诺解析程序是一个抽象操作,以
输入一个promise和一个值,我们将其表示为[[Resolve]](promise,
x)。如果x是一个可以使用的,那么它会尝试使用
x的状态,假设x的行为至少有点像
的承诺。否则,它满足值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.

这种对theables的处理允许
的promise实现互操作,只要它们暴露Promise / A + -compliant然后
方法。它还允许Promises / A +实现使用合理的方法同化
不一致的实现。

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 lets break it down:

规则是,如果 .then 处理程序中的函数返回一个值, Promise 使用该值进行解析。如果函数返回 Promise ,则下一个then子句将是 .then 返回的已解析状态无极。它的实际工作方式将在下面详细介绍:

The rule is, if the function in the .then handler returns a value, the Promise resolves with that value. If the function returns a Promise, the next then clause will be the .then of the resolved state of the returned Promise. The way it actually works is described below in more detail:

1。返回 .then 函数将是承诺的解析价值。

1. The return of the .then function will be the resolve 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 resolve of its promise will be the resolve of its next .then basically.

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"
});

这篇关于承诺待定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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