title: promise/callback hybrid function
body: <p>Im playing arround with promises and callbacks and wonder what is the correct way to write a function that returns a promise if no callback is passed.</p>
  <p>My result looks like this, but im not sure if this is correct (in the meaning of anti pattern)</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const mySuperFunction = function mySuperFunction(data, cb) {
  
      let wrapper = new Promise((resolve, reject) =&gt; {
          setTimeout(() =&gt; {
  
              if (Math.random() &gt;= 0.5) {
                  resolve(Date.now());
              } else {
                  reject(new Error("Not today..."));
              }
  
          }, 100);
      });
  
  
      if (cb) {
  
          wrapper.then((result) =&gt; {
              cb(null, result);
          }, (error) =&gt; {
              cb(error);
          });
  
      } else {
          return wrapper;
      }
  
  };
  
  
  mySuperFunction().then((time) =&gt; {
      console.log(time)
  }).catch((err) =&gt; {
      console.log(err);
  });
  
  
  mySuperFunction(null, (err, time) =&gt; {
      console.log(err, time)
  });</code></pre>
  </div>
  </div>
  </p>
  <p>Its simple: create a function and wrap the &quot;work&quot; code in a promise.
  If no callback is passed to my function, i return the wrapped promise. If a calback is passed, i wrap/call it from <code>.then(...)</code> and <code>.catch(...)</code></p>
  <p>Is this ok, or do i miss some special cases where this dosn't work ?</p>