Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript async Wait函数在第二个请求后不更改变量?_Javascript_Node.js_Asynchronous_Async Await - Fatal编程技术网

Javascript async Wait函数在第二个请求后不更改变量?

Javascript async Wait函数在第二个请求后不更改变量?,javascript,node.js,asynchronous,async-await,Javascript,Node.js,Asynchronous,Async Await,因此,我在异步等待函数方面遇到了问题。 以下是我正在做的: 我有一个异步函数,它使用wait,例如getuserdata // I am trying to check multiple users as much as possible basically. getuserdata("Swift") getuserdata("someotherperson") async function getuserdata(username){ //inside this I have 2 awa


因此,我在异步等待函数方面遇到了问题。
以下是我正在做的:


我有一个异步函数,它使用wait,例如getuserdata

// I am trying to check multiple users as much as possible basically.
getuserdata("Swift")
getuserdata("someotherperson")

async function getuserdata(username){
  //inside this I have 2 await functions which await a value from 2 different async functions. for example: user balance and user id
var userbalance = await getuserbalance(username)
var userid = await getuserid(username)
console.log(username + " has " + userbalance + " as balance and their user ID is: " userid)
//Now let's say we want to check 2 users a few times a minute to keep their balance up to date, so we run the function again
getuserdata(username)
}

//but in both of the functions (getuserbalance and getuserid) I have a web request which also uses await
// here's an example
async function getuserbalance(username){
    var request = await axios({
        url: "https://example.com/api/userbalance.php?username="+username,
        method: "GET"
    })
    return request.data
}

async function getuserid(username){
    var request = await axios({
        url: "https://example.com/api/userid.php?username="+username",
        method: "GET"
    })
    return request.data
}
这就是错误所在 前两次对我来说很好。 所以前2次它会在控制台中显示:

Swift余额为100,其用户ID为:1
其他人有4个as余额,其用户ID为:2

但在2次运行后,它会继续显示:

其他人有4个帐户作为余额,其用户ID为:2

但在大约15个请求之后,它再次切换到第一个请求。 所以它会一直说

Swift余额为100,用户ID为:1

大约15个请求,然后再次切换。 有没有办法让它每次都更新值


感谢您阅读此文。

这可能满足您的要求。请用API调用替换db

db={'p1':{'balance':300,'id':1},'p2':{'balance':540,'id':2},'p3':{'balance':800,'id':3}
用户=['p1','p2','p3'];
每名员工尝试2次;
(异步()=>{
//针对多个员工的并行测试方式
等待Promise.all(users.map(p=>getuserdata(p,尝试每个员工),然后(()=>console.log('Done Checking all users in Parallel');
//同步方式
for(让u个用户){
等待getuserdata(每个员工尝试一次);
}
log('已完成对系列中所有用户的检查');
})();
异步函数getuserdata(用户名,次){
const userbalance=await getuserbalance(用户名)
const userid=await getuserid(用户名)
log(username+“具有”+userbalance+”作为余额,其用户ID为:“+userid”)

尝试,我想进一步使用变量userbalance和userid,那么,我起草的代码是否有效,或者是否按照您的要求有效?您编写的getuserdata方法有一个无限循环。我让它具有固定的尝试次数。它不符合我的要求,我有一个数组,可以在其中添加多个Items,然后它通过这些项目循环并运行一个检查,就像我在上面解释的那样,然后我需要它再次运行,所以基本上它在函数完成后再次运行。你能检查编辑的答案并确认它吗。