您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“koa中間件處理模塊koa-compose怎么用”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“koa中間件處理模塊koa-compose怎么用”這篇文章吧。
koa-compose看起來代碼少,但是確實繞。閉包,遞歸,Promise。。。看了一遍腦子里繞不清楚。看了網上幾篇解讀文章,都是針對單行代碼做解釋,還是繞不清楚。最后只好采取一種傻瓜的方式:
koa-compose去掉一些注釋,類型校驗后,源碼如下:
function compose (middleware) { return function (context, next) { // last called middleware # let index = -1 return dispatch(0) function dispatch (i) { if (i <= index) return Promise.reject(new Error('next() called multiple times')) index = i let fn = middleware[i] if (i === middleware.length) fn = next if (!fn) return Promise.resolve() try { return Promise.resolve(fn(context, dispatch.bind(null, i + 1))); } catch (err) { return Promise.reject(err) } } } }
寫出如下代碼:
var index = -1; function compose() { return dispatch(0) } function dispatch (i) { if (i <= index) return Promise.reject(new Error('next() called multiple times')) index = i var fn = middleware[i] if (i === middleware.length) fn = next if (!fn) return Promise.resolve('fn is undefined') try { return Promise.resolve(fn(context, dispatch.bind(null, i + 1))); } catch (err) { return Promise.reject(err) } } function f1(context,next){ console.log('middleware 1'); next().then(data=>console.log(data)); console.log('middleware 1'); return 'middleware 1 return'; } function f2(context,next){ console.log('middleware 2'); next().then(data=>console.log(data)); console.log('middleware 2'); return 'middleware 2 return'; } function f3(context,next){ console.log('middleware 3'); next().then(data=>console.log(data)); console.log('middleware 3'); return 'middleware 3 return'; } var middleware=[ f1,f2,f3 ] var context={}; var next=function(context,next){ console.log('middleware 4'); next().then(data=>console.log(data)); console.log('middleware 4'); return 'middleware 4 return'; }; compose().then(data=>console.log(data));
直接運行結果如下:
"middleware 1"
"middleware 2"
"middleware 3"
"middleware 4"
"middleware 4"
"middleware 3"
"middleware 2"
"middleware 1"
"fn is undefined"
"middleware 4 return"
"middleware 3 return"
"middleware 2 return"
"middleware 1 return"
按著代碼運行流程一步步分析:
dispatch(0)
i==0,index==-1 i>index 往下
index=0 fn=f1 Promise.resolve(f1(context, dispatch.bind(null, 0 + 1)))
這就會執行
f1(context, dispatch.bind(null, 0 + 1))
進入到f1執行上下文
console.log('middleware 1');
輸出middleware 1
next()
其實就是調用dispatch(1) bind的功勞
遞歸開始
dispatch(1)
i==1,index==0 i>index 往下
index=1 fn=f2 Promise.resolve(f2(context, dispatch.bind(null, 1 + 1)))
這就會執行
f2(context, dispatch.bind(null, 1 + 1))
進入到f2執行上下文
console.log('middleware 2');
輸出middleware 2
next()
其實就是調用dispatch(2)
接著遞歸
dispatch(2)
i==2,index==1 i>index 往下
index=2 fn=f3 Promise.resolve(f3(context, dispatch.bind(null, 2 + 1)))
這就會執行
f3(context, dispatch.bind(null, 2 + 1))
進入到f3執行上下文
console.log('middleware 3');
輸出middleware 3
next()
其實就是調用dispatch(3)
接著遞歸
dispatch(3)
i==3,index==2 i>index 往下
index=3 i === middleware.length fn=next Promise.resolve(next(context, dispatch.bind(null, 3 + 1)))
這就會執行
next(context, dispatch.bind(null, 3 + 1))
進入到next執行上下文
console.log('middleware 4');
輸出middleware 4
next()
其實就是調用dispatch(4)
接著遞歸
dispatch(4)
i==4,index==3 i>index 往下
index=4 fn=middleware[4] fn=undefined reuturn Promise.resolve('fn is undefined')
回到next執行上下文
console.log('middleware 4');
輸出middleware 4
return 'middleware 4 return' Promise.resolve('middleware 4 return')
回到f3執行上下文
console.log('middleware 3');
輸出middleware 3
return 'middleware 3 return' Promise.resolve('middleware 3 return')
回到f2執行上下文
console.log('middleware 2');
輸出middleware 2
return 'middleware 2 return' Promise.resolve('middleware 2 return')
回到f1執行上下文
console.log('middleware 1');
輸出middleware 1
return 'middleware 1 return' Promise.resolve('middleware 1 return')
回到全局上下文
至此已經輸出
"middleware 1"
"middleware 2"
"middleware 3"
"middleware 4"
"middleware 4"
"middleware 3"
"middleware 2"
"middleware 1"
那么
"fn is undefined"
"middleware 4 return"
"middleware 3 return"
"middleware 2 return"
"middleware 1 return"
怎么來的呢
回頭看一下,每個中間件里都有
next().then(data=>console.log(data));
按照之前的分析,then里最先拿到結果的應該是next中間件的,而且結果就是Promise.resolve('fn is undefined')的結果,然后分別是f4,f3,f2,f1。那么為什么都是最后才輸出呢?
Promise.resolve('fn is undefined').then(data=>console.log(data)); console.log('middleware 4');
運行一下就清楚了
或者
setTimeout(()=>console.log('fn is undefined'),0); console.log('middleware 4');
整個調用過程還可以看成是這樣的:
function composeDetail(){ return Promise.resolve( f1(context,function(){ return Promise.resolve( f2(context,function(){ return Promise.resolve( f3(context,function(){ return Promise.resolve( next(context,function(){ return Promise.resolve('fn is undefined') }) ) }) ) }) ) }) ) } composeDetail().then(data=>console.log(data));
方法雖蠢,但是compose的作用不言而喻了
最后,if (i <= index) return Promise.reject(new Error('next() called multiple times'))這句代碼何時回其作用呢?
一個中間件里調用兩次next(),按照上面的套路走,相信很快就明白了。
以上是“koa中間件處理模塊koa-compose怎么用”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。