JS:处理列表时的“pre”和“post”操作使用 IIFE ?

问题描述

考虑以下将“列表”作为参数并相应地对数组执行操作的代码

# ready to test: just copy it in a file...
const create = (list) => {
    console.log('seting up for list',list)

    const install = (pool) => console.log('installing',pool)
    const nope = (pool) => void 0
    const action = (t) => console.log('action',t)  

    const api = (tasks) => {
        return {
            install: (pool) => {
                tasks.install(pool)

                // the TURN is structurally made HERE (no if...)
                tasks = { install: nope }

                return { action }       
            }
        }
    }

    let once = { install }

    return api(once)
}   


const bmp = { 'blocks-memoty-pool':  1234 }
const lists = ['a,b,c','d,e,f']
const arr = [0,1,2,3]

lists.map((l) => {
    console.log()
    const created = create(l)

    arr.map((e) => {        
        created.install(bmp).action(e)
    })
})

此处,“安装”操作仅在开始时触发一次。 'list' 中,并且仅在处理数字数组 'arr' 的最开始应用一次。

有没有办法使用 IIFE 来抽象“一次”行为?

我们可以用同样的方式进行安装后任务吗?

注意: 这个问题是关于处理的“方面”。因为它将适用于 Async/Await 用例..我必须避免类似的事情:

for(let i = 0; i < arr.length; i++) {
    if(i === 0) pre_install(nmp)
    process(arr[i])
    if(i === arr.length - 1) post_install(nmp)       
}

由于循环中的测试成本(就复杂性而言)...

问候。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)