当我没有 return 值时将回调转换为 Promise

Converting callback to Promise when I dont have the return value

我在使用 hapi 的服务器上工作并执行来自 node-rules 的规则。

我有一个回调,由 node-rulesR.execute 方法调用。作为执行 callback.

的结果,我需要 return 来自 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 没有 return 我可以使用的任何东西。我注意到在 execute 中递归调用此 function here 但没有回调不会终止。

如何将其转换为 returnPromise 的函数?

不确定我是否理解正确,但这样的事情可能会做

function exec(input) {
    const { medicineType, facts: data } = input
    const R = new RuleEngine()
    R.register(rules)

    return new Promise(function(resolve, reject) {
        if (medicineType !== 'generic') {
            const facts = {
                data
            }
            R.execute(facts, function(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'
                    )
                }
                resolve('<return here whatever you want>')
                // or
                // reject('<return some error>')
            });
        } else {
            reject('<return some error>')
        }
    })
}

在浏览其他问题的一些答案时,我想起了 $.deferredQ.defer API,我找到了一个类似于它们的解决方案:

  1. 创建延迟

  2. 将延迟传递给回调

  3. 使用deferred并解决承诺

  4. 最重要的是,return promise 是由 deferred

  5. 创建的

I did not want a custom Promise implementation or to monkey-patch the Promise API. If the latter is not a problem, there are several modules on npm which do this and polyfill Promise.defer.

defer函数是from here

代码现在看起来像:

/* eslint-disable promise/param-names */
function defer () {
  let resolve, reject
  const promise = new Promise(function (...args) {
    resolve = args[0]
    reject = args[1]
  })
  return {resolve, reject, promise}
}

/* eslint-enable promise/param-names */

const makeCallback = deferred => 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'
    )
  }
  deferred.resolve(descision)
}

function exec (input) {
  const {medicineType, facts: data} = input
  const R = new RuleEngine()
  R.register(rules)
  if (lenderType !== 'generic') {
    const facts = {
      data
    }
    const deferred = defer()
    const callback = makeCallback(deferred)
    R.execute(facts, callback)
    return deferred.promise
  }
}