Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local async = {}
- --[[
- 时间原因,未考虑函数参数,返回结果及错误处理的情况
- ]]
- -- task(callback) callback是约定
- function async.parallel(tasks)
- local len = #tasks
- if len < 1 then
- return
- end
- for _, func in ipairs(tasks) do
- func()
- end
- end
- function async.serial(tasks)
- local len = #tasks
- if len < 1 then
- return
- end
- local index = 1
- local iterate
- iterate = function()
- local task = tasks[index]
- task(
- function ()
- index = index + 1
- if index <= len then
- iterate()
- end
- end
- )
- end
- iterate()
- end
- local function invoke_cb(cb)
- if nil ~= cb then
- cb()
- end
- end
- -- 可以为异步函数
- local function func1(callback)
- invoke_cb(callback)
- print("func1")
- end
- -- 可以为异步函数
- local function func2(callback)
- invoke_cb(callback)
- print("func2")
- end
- local my_tasks = {func1, func2}
- async.serial(my_tasks)
- async.parallel(my_tasks)
Add Comment
Please, Sign In to add comment