You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
constfetchCourse=(id,args)=>fetchApi(`${courseEndpoint}/id`,args);constfetchMetadata=args=>fetchApi(metadataEndpoint,args);constgetAllCourseData=({ id, tags })=>({type: SET_DATA,value: Promise.all([fetchCourse(id),fetchMetadata(tags)]).then(([coursesData,tagsData])=>({courses: coursesData,metadata: tagsData}))})// or if you want to dispatch 2 different actions:constgetAllCourseData=({ id, tags })=>Promise.all([fetchCourse(id).then(data=>({type: SET_COURSE,value: data.course})),fetchMetadata(tags).then(data=>({type: SET_METADATA,value: data.tags})),]);// which is more or less equivalent to:constgetAllCourseData=({ id, tags })=>dispatch=>Promise.all([fetchCourse(id).then(data=>dispatch({type: SET_COURSE,value: data.course})),fetchMetadata(tags).then(data=>dispatch({type: SET_METADATA,value: data.tags})),]);// except this one dispatch those actions as early as possible// for all those cases, it will error if one of the sub-promise errors
letcontroller;constmyTask=args=>dispatch=>{if(controller){// if a previous one exist, cancel itcontroller.abort();}controller=newAbortController();returnfetchApi(endpoint,{ ...args,signal: controller.signal}).then(data=>dispatch({type: MY_ACTION,value: data}));};// you could make any promise-based function abortable, not just this fetch wrapper
constplayLevelOne=()=>value/*score or promise returning score*/;constplayLevelTwo=()=>value/*score or promise returning score*/;constplayLevelThree=()=>value/*score or promise returning score*/;constgame=()=>dispatch=>[playLevelOne,playLevelTwo,playLevelThree].reduce(async(_,playLevel)=>{awaitp;constvalue=awaitplayLevel();dispatch({type: SET_SCORE, value });},Promise.resolve());// or without awaitconstgame=()=>dispatch=>[playLevelOne,playLevelTwo,playLevelThree].reduce((p,playLevel)=>{returnp.then(playLevel).then(value=>dispatch({type: SET_SCORE, value }));},Promise.resolve());// both stop and throw the error as soon as an action throws