aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/nodeify.js
blob: 51d89a8fb905978bb3f72d1f61603761a9efe170 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module.exports = function (promiseFn) {
  return function () {
    var args = []
    for (var i = 0; i < arguments.length - 1; i++) {
      args.push(arguments[i])
    }
    var cb = arguments[arguments.length - 1]

    const nodeified = promiseFn.apply(this, args)

    if (!nodeified) {
      const methodName = String(promiseFn).split('(')[0]
      throw new Error(`The ${methodName} did not return a Promise, but was nodeified.`)
    }
    nodeified.then(function (result) {
      cb(null, result)
    })
    .catch(function (reason) {
      cb(reason)
    })

    return nodeified
  }
}