js洋葱模型,支持异步async
生活随笔
收集整理的這篇文章主要介紹了
js洋葱模型,支持异步async
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
const sleep=require('./utils/sleep')
//js洋蔥模型,支持異步async
function compose (middleware) {
return async function () {
let args = arguments
await dispatch(0);
async function dispatch (i) {
const fn = middleware[i]
if (!fn) return null
await fn( ...args,async function next () {
await dispatch(i + 1)
})
}
}
}
//demo
let middleware = []
middleware.push(async (next) => {
console.log(0)
await next()
console.log(3)
})
middleware.push(async (next) => {
console.log(1)
await sleep(2000)
await next()
console.log(1.1)
})
middleware.push(() => {
console.log(2)
})
const func=compose(middleware)
func()
總結
以上是生活随笔為你收集整理的js洋葱模型,支持异步async的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绝不妥协!一加Ace 2搭载性能三件套
- 下一篇: 马斯克睡觉时间公开!太可怕了 比你聪明还