kriskowal / q

A promise library for JavaScript
MIT License
14.93k stars 1.2k forks source link

Helpers not interoperable #154

Closed ForbesLindesay closed 11 years ago

ForbesLindesay commented 11 years ago

It would be nice if Q.all() accepted any other promises-aplus promises.

function delay() {
  var a = require('promises-a');
  var def = a();
  setTimeout(function () { def.fulfill('foo'); }, 100);
  return def.promise;
}
Q.all([delay()])
  .then(function (res) {
    console.log(res);
  });

Ideally this would log ['foo'] but it actually logs the still unresolved promise.

kriskowal commented 11 years ago

It would be a simple matter to remove the isFulfilled optimization.

ForbesLindesay commented 11 years ago

Hmm, that would make Q.all unusable in QEJS because it makes the performance unbearable.

How about changing isFulfilled so that it's interoperable? We don't want to change isPromise because other parts of the library already use that to mean 'is a Q promise'.

/**
 * @returns whether the given object is a value or fulfilled
 * promise.
 */
Q.isFulfilled = isFulfilled;
function isFulfilled(object) {
    var val = valueOf(object);
    return !(val && typeof val === 'object' && typeof val.then === 'function');
}
ForbesLindesay commented 11 years ago

Thanks