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