什麼是建立在Javascript流暢接口(node.js中)與異步的JavaScript流利(節點)接口(使用deferreds)
obj.function1().function2().function3();
其中函數是異步方法的最佳方法是什麼?
有一個叫電鋸模塊,但如果是possilble做deferreds和承諾(https://github.com/kriskowal/q)
UPD:鏈q.js
obj.function1().then(obj.function2)
//inside obj.function2 "this" context is lost,
//and code is actually broken
obj.function1().then(funciton(){
obj.function2() // <-- "this" context is OK
})
謝謝,但據我所知,有承諾,我可以很容易地做出這樣obj.function1鏈(),然後(obj.function2),但我對流利的鏈條更感興趣。 – WHITECOLOR
簡單的答案是承諾只暴露承諾方法,並且這些方法規定鏈式語法。 –
看來,當我調用.then(obj.function2)而執行「this context」的函數2丟失(「this」並不指obj) – WHITECOLOR