从另一个函数中的promise内部返回一个值[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从另一个函数中的promise内部返回一个值[重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

我正在尝试从firebase中的getUserRecordFromAuth函数内部返回userRecord,并使用它从Firebase中的Authentication中获取用户的UID

问题是userRecord.uid总是返回undefined,即使我在getUserRecordFromAuth()中返回promise的值。是什么赋予了?

const getUserData = mobile => {
    const userRecord = getUserRecordFromAuth(mobile);

    console.log('uid', userRecord.uid);
}

const getUserRecordFromAuth = mobile => {

    const promise = auth.getUserByPhoneNumber(mobile);

    return promise.then(userRecord => {
        return userRecord;
    }).catch(error => {
        console.log(error);
    });
}
答案

当您返回Promise以获得结果时,您需要使用.then()

const getUserData = mobile => {
    getUserRecordFromAuth(mobile)
     .then((userRecord) => console.log('uid', userRecord.uid));
}

以上是关于从另一个函数中的promise内部返回一个值[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在异步函数内部,从回调函数返回值返回 Promise(undefined) [重复]

在异步函数内部,从回调函数返回值返回 Promise(undefined) [重复]

ES6+--》熟知JS中的async函数

如何将promise的返回值推送到数组中? [重复]

为啥 async/await Axios 请求不会返回 Data 而不是 Promise 对象 [重复]

将函数的进度输出到执行 Promise 内部的控制台 [重复]