如何获取javascript承诺的返回值? [英] How to get the return value of a javascript promise?

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

问题描述

我很难理解如何在 Javascript 中获取 promise 的值,以便能够检查它是真还是假.

I am struggling to understand how to get the value of a promise within Javascript to be able to check whether it is true or false.

let valid = validateForm();

if ( valid === true ) {
}

如果我 console.log 有效变量,它返回以下内容:

If I console.log the valid variable, it returns the following:

Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: true 

在我的 if 语句中,我试图检查 promise 值是否为真,但是我不知道如何访问它:/有人可以建议如何检查吗?

Within my if statement, I am trying to check whether the promise value is true, however I don't know how to gain access to this :/ Could anyone please advise how to check this?

谢谢

推荐答案

您可以通过 .thenawait 获得它.

You get it either with .then or await.

let valid = validateForm();

valid.then(function(valid) {
 if (valid) {

 }
})

async function submit () {
  const valid = await validateForm();

  if (valid) {

  }
}
``

这篇关于如何获取javascript承诺的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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