* Attaches a node-style callback to a promise, ensuring the callback is * called for either fulfillment or rejection. Returns a promise with the same * state as the passed-in promise. * * @example * var deferred = when.defer(); * * function callback(err, value) { * // Handle err or
(promise, callback)
| 228 | * @returns {Promise} A promise with the same state as the passed-in promise. |
| 229 | */ |
| 230 | function bindCallback(promise, callback) { |
| 231 | promise = when(promise); |
| 232 | |
| 233 | if (callback) { |
| 234 | promise.then(success, wrapped); |
| 235 | } |
| 236 | |
| 237 | return promise; |
| 238 | |
| 239 | function success(value) { |
| 240 | wrapped(null, value); |
| 241 | } |
| 242 | |
| 243 | function wrapped(err, value) { |
| 244 | setTimer(function () { |
| 245 | callback(err, value); |
| 246 | }, 0); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Takes a node-style callback and returns new function that accepts a |
no test coverage detected
searching dependent graphs…