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
classFlow{constructor(task){this.task=task}asyncrun(cb){awaitthis.task()cbinstanceofFunction&&cb()}}construnFlow=async(flow)=>{if(flowinstanceofArray){for(constitemofflow){awaitrunFlow(item)}}elseif(flowinstanceofFunction){awaitrunFlow(flow())}elseif(flowinstanceofFlow){awaitflow.run()}elseif(flowinstanceofPromise){awaitflow}else{// static val, do nothingreturn}}constcollectFlow=(flow,list=[])=>{list.push(()=>runFlow(flow))returnlist}exportconstcreateFlow=(flow)=>{returnnewFlow(()=>runFlow(collectFlow(flow,[])))}
Activity
sisterAn commentedon Sep 9, 2020
在本题中,
createFlow以一个数组作为参数,数组参数可有以下几种类型:createFlow步骤一:扁平化
因为
effects的执行只需要支持串行,所以我们可以把数组扁平化一下步骤二:run执行
createFlow并不是直接执行,而是.run()之后才会开始执行步骤三:异步函数
因为参数中有异步函数,这里使用
await执行步骤四:支持嵌套
使用
isFlow来判断当前是否是嵌套执行其实是可以使用
class来实现,用instanceof来判断是否是嵌套,代码实现如下:完整代码:两种
参考:https://juejin.im/post/6860646761392930830
m7yue commentedon Sep 10, 2020
m7yue commentedon Sep 10, 2020
之前会错意了,思路一样,利用 Promise 异步串行
kexiaofu commentedon Sep 11, 2020
其实我还有一个思路:
收集参数部分相同;
在 run 的时候,判断 task.then 是否是可执行的方法,请看代码
xiaowuge007 commentedon May 12, 2021
NoBey commentedon Mar 23, 2022
Athaoo commentedon Aug 8, 2023
不简洁但是把步骤拆分成了collect和run两个阶段,不进行flat
front-end-captain commentedon Jun 10, 2024