No idea what the following example extracted from the doc means:
function applicationFunction(arg1) {
var deferred = Promise.pending(); //Or Q.defer() in Q
libraryFunction(arg1, function (err, value) {
if (err) {
deferred.reject(err);
} else {
deferred.fulfill(value);
}
});
return deferred.promise;
}
Promise.pending()? what does it return? It seems it returns:{ promise:
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined },
resolve: [Function: deferResolve],
reject: [Function: deferReject] }
but that is not documented.
Also, there is no fulfill() function in the returned object.
This looks like very old documentation(from v0 and v1 era) , if this is in up-to-date docs then that needs to be fixed.
It is: http://bluebirdjs.com/docs/anti-patterns.html#explicit-construction-anti-pattern
Fixing. Thanks for the find.
Nice, but... how am I supposed to check the new syntax in the doc? :)
hey @benjamingr - current docs still include Promise.pending() discussing when to use deferred:
//setTimeout that returns a promise
function delay(ms) {
var deferred = Promise.pending();
setTimeout(function(){
deferred.fulfill();
}, ms);
return deferred.promise;
}
Running this code gives me TypeError: undefined is not a function.
I'm new to promises and Bluebird, just wanted some clarification! Thanks!
Hi @vicfriedman apologies - can you send me a link to the documentation showing this so it can be corrected?
The correct use would be to use Promise.delay() for timeouts and if we wanted to promisify a method not returning a Promise to use the promise constructor:
function delay(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function() { resolve(); }, ms);
});
}
Or more compactly with modern syntax:
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
Thanks, fixed there - @petkaantonov can you run a docs build?
thanks for the quick response @benjamingr ! :)
pushed
Looks like wiki has dupe information that needs updating - https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns