有没有一种方法可以将[[Promise Result]]保存为变量? [英] Is there a way to save [[Promise Result]] as a variable?

查看:3663
本文介绍了有没有一种方法可以将[[Promise Result]]保存为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问[[Promise Results]]并将其保存为变量。最终目标,我只需要.then语句的结果并将其用于其他功能。如果还有其他更好的方法可以使用,请告诉我,我是JavaScript的新手,如果您能向我解释它而不只是转储代码,那就太好了。感谢提前
这是提取请求

I am trying to access [[Promise Results]] and save it as a variable. The end goal I just want the result from the .then statement and use it in other function. If there is another better way to do it please let me, I'm new to JavaScript, so it would be awesome if you could explain it to me than just dump code. Thanks is advance Here is the fetch request

function currentloginid() {
    return fetch('http://localhost/gaq/api/api.php?action=userid', {
       method: 'GET',
    })
    .then(function(response) {
        return response.json();
    })
    .then(function(data) {
        var userid = JSON.parse(data);
        console.log(userid);
        return userid;
    })
}

下面的代码是我在控制台登录该函数时在另一个函数中

The code below is when I console log the function in another function

Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: 1


推荐答案

解决此问题的方法有3种:

There are 3 ways for solving this:


  1. 由于您返回了承诺,请使用。然后获取返回的值。

  1. Since you return a promise, use .then to get the returned value.

function currentloginid() {
  return fetch('http://localhost/gaq/api/api.php?action=userid', {
      method: 'GET',
    })
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      var userid = JSON.parse(data);
      console.log(userid);
      return userid;
    })
}

currentloginid().then(value => console.log(value));


  1. 您已经有一个。然后,将外部变量设置为该值。但是,此解决方案不是很好,因为您可能会遇到未设置 myValue 的情况。

  1. In one of the .then you already have, set an outside variable to the value. However this solution is not good since you could encounter situation where myValue is not set.

let myValue;

function currentloginid() {
  return fetch('http://localhost/gaq/api/api.php?action=userid', {
      method: 'GET',
    })
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      var userid = JSON.parse(data);
      console.log(userid);
      myValue = userid
      return userid;
    })
}

currentloginid();
console.log(myValue);


  1. 使用语法糖 async await 来等待。返回的值。我认为这种方法更具可读性和易用性(在后台与选项1相同)。

  1. Use the syntactic sugar async await to "await" for the value to return. I think that this approach is much more readable and easy to use (behind the scenes it's the same as option 1).


function currentloginid() {
  return fetch('http://localhost/gaq/api/api.php?action=userid', {
      method: 'GET',
    })
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      var userid = JSON.parse(data);
      console.log(userid);
      return userid;
    })
}

console.log(await currentloginid());

这篇关于有没有一种方法可以将[[Promise Result]]保存为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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