获取价值异步功能javascript

时间:2018-06-08 02:55:33

标签: reactjs firebase

变量检查总是返回0,authDefault.onAuthStateChanged函数返回后调用firebase数据库,如何修复?我想在firebase中获取值并设置为变量检查

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个:

async function checkLogin() {

    const check = await new Promise(function (resolve) {

        authDefault.onAuthStateChanged(function (user) {

            if (user) {
                resolve(1);
            }
            else {
                resolve(2);
            }
        });
    });

    return check;
}