I know how to create a promise in Kris Kowal's q with var defer = Q.defer();
, calling defer.resolve();
and/or defer.reject()
and return defer.promise
. But reading the docs, it seem's there is an alternative way to create a promise...
From the docs:
Q.Promise(resolver)
Synchronously calls
resolver(resolve, reject, notify)
and returns a promise whose state is controlled by the functions passed to resolver. This is an alternative promise-creation API that has the same power as the deferred concept, but without introducing another conceptual entity.If resolver throws an exception, the returned promise will be rejected with that thrown exception as the rejection reason.
This is, what I've tried:
function () {
return Q.Promise(function (resolve, reject) {
(...do something...)
resolve(5); // or: reject(error);
});
}
But this doesn't work as expected!
Can someone give an example, how to use Q.Promise
?
UPDATE: Thanks for downvoting! I asked for an usage example, therefore a simple "you use it in a correct way" is more helpful! Btw: it fails silently and yes, I attached an error handler!
The reason, why the function is unnamed, is that I use it together with map
and reduce
to create a delayed chain of promises, but it seems, the resolver functions are never called... Therefore I asked for a (again) usage example...