简体   繁体   English

您如何将“for”转换为异步模式?

[英]How do you turn a "for" into an async mode?

export function RecommendList(data) {
    return (dispatch, getState) => {
        let db = loadFB().firestore();
        let query = db.collection('users').where("recommend", ">", "0").orderBy("recommend", "asc")
        let user_list = []; 
        let uid_arr=[];
        let result = [];
        query.get().then(async docs => {
            docs.forEach(doc => {
                const recommender = doc.data();
                const recommend_id = recommender.recommend;
                const recommend_person = recommender.displayName;                                
                user_list.push({id : recommend_id, recommend_person : recommend_person });
            })        

            uid_arr = getRecommendList(user_list);

            console.log("getRecommendList",uid_arr);

            for(let i = 0; i < uid_arr.length; i++) {
                const user_doc = await db.collection('users').doc(uid_arr[i].id).get();
                console.log(user_doc.data());
                let user_info = user_doc.data()
                user_info.betball_dupli_count = uid_arr[i].count;
                user_info.recommend_person = uid_arr[i].person;
                console.log('displayname' , user_info.displayName , 'betball count',user_info.betball_dupli_count,'person',user_info.recommend_person);
                result.push(user_info);
            } 
            console.log('result!', result);
            dispatch({
                type: types.SET_USER_LIST,
                data: result,
                page: 1
            })
        })
    }
}


I work on importing data from the Fire Store and sending it to the dispatch.我致力于从 Fire Store 导入数据并将其发送给调度员。 By the way, I want to make a code that increases the efficiency of this work by asynchronous mode in javascript.顺便说一句,我想在javascript中通过异步模式制作一个提高这项工作效率的代码。 I'd like to know how to wait and process a form asynchronously.我想知道如何异步等待和处理表单。

In short, how to turn "for" of this code into async mode!简而言之,如何将这段代码的“for”变成异步模式!

With minimal changes to your existing code:对现有代码进行最少的更改:

let userQueries = [];
for (let i = 0; i < uid_arr.length; i++) {
    userQueries.push(
        db.collection('users').doc(uid_arr[i].id).get()
        .then(user_doc => {
            console.log(user_doc.data());
            let user_info = user_doc.data()
            user_info.betball_dupli_count = uid_arr[i].count;
            user_info.recommend_person = uid_arr[i].person;
            console.log('displayname', user_info.displayName, 'betball count', user_info.betball_dupli_count, 'person', user_info.recommend_person);
            result.push(user_info);

        })
    )
}
await Promise.all(userQueries);

The outer for loop will just initiate the queries and the then part of each one will run when the specific query is complete.外部for循环只会启动查询,当特定查询完成时,每个查询的then部分将运行。 Outside of the for loop, the Promise.all call will wait for all of the queries to complete before continuing.for循环之外, Promise.all调用将在继续之前等待所有查询完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM