(func)
| 104 | } |
| 105 | |
| 106 | function toPromise(func) { |
| 107 | //create the function we will be returning |
| 108 | return function (...args) { |
| 109 | // Clone arguments |
| 110 | args = clone(args); |
| 111 | var self = this; |
| 112 | // if the last argument is a function, assume its a callback |
| 113 | var usedCB = (typeof args[args.length - 1] === 'function') ? args.pop() : false; |
| 114 | var promise = new Promise(function (fulfill, reject) { |
| 115 | var resp; |
| 116 | try { |
| 117 | var callback = once(function (err, mesg) { |
| 118 | if (err) { |
| 119 | reject(err); |
| 120 | } else { |
| 121 | fulfill(mesg); |
| 122 | } |
| 123 | }); |
| 124 | // create a callback for this invocation |
| 125 | // apply the function in the orig context |
| 126 | args.push(callback); |
| 127 | resp = func.apply(self, args); |
| 128 | if (resp && typeof resp.then === 'function') { |
| 129 | fulfill(resp); |
| 130 | } |
| 131 | } catch (e) { |
| 132 | reject(e); |
| 133 | } |
| 134 | }); |
| 135 | // if there is a callback, call it back |
| 136 | if (usedCB) { |
| 137 | promise.then(function (result) { |
| 138 | usedCB(null, result); |
| 139 | }, usedCB); |
| 140 | } |
| 141 | return promise; |
| 142 | }; |
| 143 | } |
| 144 | |
| 145 | function logApiCall(self, name, args) { |
| 146 | /* istanbul ignore if */ |
no test coverage detected
searching dependent graphs…