index.js 408 B

123456789101112131415161718192021
  1. "use strict"
  2. var next = (global.process && process.nextTick) || global.setImmediate || function (f) {
  3. setTimeout(f, 0)
  4. }
  5. module.exports = function maybe (cb, promise) {
  6. if (cb) {
  7. promise
  8. .then(function (result) {
  9. next(function () { cb(null, result) })
  10. }, function (err) {
  11. next(function () { cb(err) })
  12. })
  13. return undefined
  14. }
  15. else {
  16. return promise
  17. }
  18. }