值未插入到数组中,数组返回空白值 [英] Value is not getting inserted in the array,array returns blank values

查看:73
本文介绍了值未插入到数组中,数组返回空白值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,问题是obj未插入数组arr中。

I have following code , the problem is that obj is not inserted in the array arr.

let arr=[];
let frommilisec=1620000000;
let tomilisec=4680000000;
let slotmilisec=900000;
while(frommilisec<=tomilisec)
{
    let timeslot_milisec=frommilisec+slotmilisec;

    clinicslotsfunc(timeslot_milisec,maximumAppointment,clinicid,dated).then(results=>
    {
        let obj=results[0];
        console.log(obj);   // this displays { slot: '12:30:00', isbooked: 1 }
        arr.push(obj);
    });    
    console.log(arr)   //this logs blank array
    frommilisec=frommilisec+timeslot_milisec;
}

结果[0] 包含数据 {插槽:'12:30:00',已预订:1}

推荐答案

Promise是异步操作,因此您需要 await 作为异步操作的结果:

Promise is an async operation, so you need to await a result of asynchronous operation:

async yourmethod()
{
    let arr=[];
    let frommilisec=1620000000;
    let tomilisec=4680000000;
    let slotmilisec=900000;
    while(frommilisec<=tomilisec)
    {
        let timeslot_milisec=frommilisec+slotmilisec;    
        let result = await clinicslotsfunc(timeslot_milisec
            , maximumAppointment, clinicid, dated);

        arr.push(result);
    });    
    console.log(arr);
    frommilisec=frommilisec+timeslot_milisec;
    return arr;
}

这篇关于值未插入到数组中,数组返回空白值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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