caolan / async

Async utilities for node and the browser
http://caolan.github.io/async/
MIT License
28.18k stars 2.41k forks source link

Async parallel - Cannot read property 'Symbol(Symbol.toStringTag)' of undefined #1745

Closed tiggem1993 closed 3 years ago

tiggem1993 commented 3 years ago

Hi, I am trying to work with async in node js, to control the flow of execution of some functions. In the code below I have three declared functions, where getMyInfo is main function which call two other function getMyPersonalInfo and getMyOfficialInfo. These two functions provide my personal and official info via fetching my APIs. Therefore I can execute further once both APIs return the data. That's why I have used async's parallel method.

import async from 'async';
import {ajaxCall} from '../api';
import {GET, DISCOVERY, POST} from '../constants/apiMethods';

export const getResult = (params) => {
    return (dispatch) => {
        async.parallel([
            dispatch(getMyPersonalInfo(params, (isSuccess) => {
                callback(null, 'My personal info');
            })),
            dispatch(getMyOfficialInfo(params, (isSuccess) => {
                callback(null, 'My official info');
            })),
        ], (err, results) => {
            //Do something after all API response fetched            
            if (err) {
                console.log("ERROR");
            } else {
                console.log("SUCCESS :", results); //Expected Result ['My personal info','My official info']
            }
        })
    }
}

export const getMyPersonalInfo = (params, isSuccess) => {
    return (dispatch) => {
        const userID = params.userid;
        const apiName = `api/${userID}/personalinfo`;
        dispatch(
            ajaxCall(GET, '', apiName, (resp) => {
                if (!!resp.callBackStatus) {
                    //Store data in reducer 
                    isSuccess(true);
                } else {
                    isSuccess(false);
                }
            })
        )
    }
}

export const getMyOfficialInfo = (params, isSuccess) => {
    return (dispatch) => {
        const userID = params.userid;
        const apiName = `api/${userID}/officialinfo`;
        dispatch(
            ajaxCall(GET, '', apiName, (resp) => {
                if (!!resp.callBackStatus) {
                    //Store data in reducer 
                    isSuccess(true);
                } else {
                    isSuccess(false);
                }
            })
        )
    }
}

So that I got error

Cannot read property 'Symbol(Symbol.toStringTag)' of undefined

I have ported according to documentation which is mentioned below

https://caolan.github.io/async/v3/docs.html#parallel

Note: I am using axios and redux in ReactNative and these functions are define in my Action library.

To sum up, please let me know what could be the best way to fix this?

Thanks in advance!

aearly commented 3 years ago

Does dispatch() return an async function?

tiggem1993 commented 3 years ago

Does dispatch() return an async function?

Hi @aearly , I am not sure, but since I have change my logic and the bug is resolved. You can go through my code as follows:

import async from 'async';
import {ajaxCall} from '../api';
import {GET, DISCOVERY, POST} from '../constants/apiMethods';

export const getResult = (params) => {    
    return(dispatch) => {
        async.parallel([
        function personalInfo(callback){
                     dispatch(getMyPersonalInfo(params, (personalInfoCallBack)=>{
            callback(personalInfoCallBack, 'My personal info');
                    }))
        },
        function officialInfo(callback){
                     dispatch(getMyOfficialInfo(params, (officialInfoCallBack)=>{
            callback(officialInfoCallBack, 'My official info');
                     }))
        },                      
        ],(err, results)=>{
            //Do something after all API response fetched            
            if(err){
                console.log("ERROR");
            }else {                
                console.log("SUCCESS :",results);//Expected Result ['My personal info','My official info']
            }
        })        
    }
}

export const getMyPersonalInfo = (params, personalInfoCallBack) => {    
    return(dispatch) => {
        const userID = params.userid;        
        const apiName = `api/${userID}/personalinfo`;
        dispatch(
            ajaxCall( GET, '', apiName, (resp) => {
        personalInfoCallBack(resp.callBackStatus);                  
            })            
        )
    }
}

export const getMyOfficialInfo = (params, officialInfoCallBack) => {    
    return(dispatch) => {
        const userID = params.userid;        
        const apiName = `api/${userID}/officialinfo`;
        dispatch(
            ajaxCall( GET, '', apiName, (resp) => {                
                officialInfoCallBack(resp.callBackStatus);
            })            
        )
    }
}

So that I got error removed, Thanks