JavaScript - Return promise AND/OR call callback?

This could be simplified if you simply always used the promise, which you're always creating anyway:

export function doSomeAsync(options, callback) {
    const promise = new Promise((resolve, reject) => {
        const check = (options.num === 1) ? true : false;
        setTimeout(() => {
            if (check) {
                resolve("Number is 1");
            } else {
                reject(new Error("Number is not 1"));
            }
        }, 1000);
    });

    if (callback && typeof callback == 'function') {
        promise.then(callback.bind(null, null), callback);
    }

    return promise;
}

Your function is always promise-based, also in the fact that it always returns a promise. The caller is simply free to ignore that. The callback argument is merely a "legacy fallback interface" (or "alternative interface" if you prefer) to using that promise.


You could get rid of all edge cases by always returning a promise, and define a default callback (a callback-shaped identity function) that handles the no-callback-supplied case:

const genericAsync = (stuff, callback = (e, i) => e || i) => new Promise(
  (resolve, reject) => doStuffWith(stuff, resolve, reject)
)
  .then(response => callback(null, response))
  .catch(callback);