反应本机不等待函数的返回值

时间:2018-06-21 12:31:38

标签: javascript react-native react-native-android

我在文件中创建了一个函数,我想从函数中返回值,然后在if条件下使用该值,但我无法定义,这是我的可导入文件代码:

export const check_student_in_db = (st_idd) => {
//check student is in attendence list
var today = new Date();
let date = today.getDate() + "/" + parseInt(today.getMonth() + 1) + "/" + today.getFullYear();
let slash_removed = date.replace(/\//g, "")
AsyncStorage.getItem('student_status', (err, result) => {
        if (!err && result != null) {
            //console.warn('note a null');
            let db_data = JSON.parse(result); // store db data of attendence
            db_dataa = JSON.parse(result);
            // concat db data with new data
            let ext = db_data[slash_removed];            //check data exist with today date
            //console.warn('get a test ' + ext);
            if (ext) {
                //search for student in dabase json data using student id
                ext.map(function (item) {
                    if(item.st_id == st_idd){
                        //console.warn(st_idd);
                        return st_idd;
                    }else {
                        return false;
                    }
                });
            } else {
                return false;
            }
        }
        else {
           return false;
        }
    });
}

这是我使用此功能的代码

 import {insert_st_attendence, check_student_in_db} from './functions/functions';
     load_memory = async () => {
            const status = await check_student_in_db('18ds1');
            console.warn(status);
            if (status) {
                console.warn(status);
            }
            return true;
        }

2 个答案:

答案 0 :(得分:1)

我的功能很简单,这是保存数据:

export async function saveData(key, value) {
    try {
        await AsyncStorage.setItem(key, value);
    } catch (error) {
        console.log('Error when saving data: ' + error.toString())
    }
}

函数getData

export async function getData(key, callback) {
    let data = await AsyncStorage.getItem(key);
    let result = JSON.parse(data);
    callback(result);
}

并在项目中的任何地方使用它

import {getData} from "...link to file/";

getData("Your Key", function (result) {
            // Your code here
  })

希望获得帮助

答案 1 :(得分:0)

首先,等待表达式使异步函数的执行暂停,直到Promise被解决为止,即实现或拒绝。 因此,在您的check_student_in_db函数中,您需要返回Promise

export const check_student_in_db = (st_idd) => {
    //check student is in attendence list
    return new Promise((resolve, reject) => {
           var today = new Date();
           let date = today.getDate() + "/" + parseInt(today.getMonth() + 1) + "/" + today.getFullYear();
           let slash_removed = date.replace(/\//g, "")
           AsyncStorage.getItem('student_status', (err, result) => {
                    if (!err && result != null) {
                            //console.warn('note a null');
                            let db_data = JSON.parse(result); // store db data of attendence
                            db_dataa = JSON.parse(result);
                            // concat db data with new data
                            let ext = db_data[slash_removed];            //check data exist with today date
                            //console.warn('get a test ' + ext);
                            if (ext) {
                                    //search for student in dabase json data using student id
                                    ext.map(function (item) {
                                            if(item.st_id == st_idd){
                                                    //console.warn(st_idd);
                                                    resolve(st_idd);
                                            }else {
                                                reject(false);
                                            }
                                    });
                            } else {
                                reject(false)
                            }
                    }
                    else {
                        reject(false)
                    }
            });
    }