Invoke
Packhouse Invoke
Action
action最後的結果最由最後的參數(callback)呈現,第一個參數是錯誤,第二個參數為運算結果。
如果你的程式是同步的,那Action是完全同步的,但仍建議把全部的函式視為非同步。
packhouse.tool('math/sum').action(10, 20, (error, result) => {
console.log(result) // 30
})Promise
如果使用Promise,則回傳Promise物件。
packhouse.tool('math/sum').promise(10, 20).then(result => {
console.log(result) // 30
})Per Invoke
在執行Invoke前可以註冊一些行為。
Pack
參數預處理,你可以在宣告之前先注入參數:
packhouse
.tool('math/sum')
.pack(10)
.action(10, (error, result) => {
console.log(result) // 20
})In Include
在Install中引用tool時只要給予額外的參數也可以運行pack:
const tool = {
install({ include }) {
include('sum').tool('sum', 10)
}
}在Include中引用line的後續參數也有pack的功能:
const tool = {
install({ include }) {
include('math').line('math', 10)
},
handler(self) {
self.line('math')()
.action((error, result) => {
console.log(result) // 10
})
}
}No Good
當錯誤處理時,由noGood接收。
Action
當宣告noGood後action的第一個參數會被移除。
packhouse
.tool('math/sum')
.noGood(e => console.log(e))
.action('1', '2', result => {
// 這裡不工作
})Promise
當宣告noGood後promise不會被reject。
嚴格來說如果你使用的是Promise請使用
reject而不是noGood
packhouse
.tool('math/sum')
.noGood(e => console.log(e))
.promise('1', '2')
.then(() => {
// 這裡會執行
})你可以運用第二個參數來強行宣告為reject。
packhouse
.tool('math/sum')
.noGood(e => console.log(e), { reject: true })
.promise('1', '2')
.then(() => {
// 這裡不會執行
})
.catch(() => {
// 這裡會執行
})Always
無論如何都會執行,事件發生在Action之後。
packhouse
.tool('math/sum')
.always(({ result, success, context }) => {
console.log(result) // 30
console.log(success) // true
console.log(context.id) // uuid, 每次執行都會配發一個新的id
console.log(context.caller) // 如果該次呼叫是由另一個tool調用,可以得知是哪個id呼叫
})
.promise(10 , 20)Promise
運行Promise時請使用finally而不是alawys。
Last updated
Was this helpful?