我正在使用hapi
的服务器上工作,并执行node-rules
的规则。当我没有返回值时将回调转换为Promise
我有一个回调,从node-rules
的R.execute
方法调用。作为执行callback
的结果,我需要从exec
方法返回Promise
。
代码
const callback = data => {
const {matchPath, result} = data
descision.setMatchPath(matchPath)
if (!result) {
descision.addMessage(
'No match could be found in the rules provided, either incorrect or non-matching information was provided'
)
}
}
function exec (input) {
const {medicineType, facts: data} = input
const R = new RuleEngine()
R.register(rules)
if (medicineType !== 'generic') {
const facts = {
data
}
R.execute(facts, callback)
}
}
我从源代码中发现R.execute
不返回任何东西,我可以使用。我注意到在execute
中递归调用function here,但没有回调就不会终止。
如何将其转换为返回Promise的函数?
你为什么要返回,而不是用'callback'功能工作了'Promise'? – ishmaelMakitla
@ishmaelMakitla'hapi'服务器正在等待规则引擎的结果,以便它可以响应一个值,所以我需要返回一个'Promise'。 – vamsiampolu
'execute'可能不会返回任何东西,但它会调用给它的'callback'。这还不够吗? – Bergi