Bluebird: What is Promise.pending()?

Created on 16 Dec 2015  路  12Comments  路  Source: petkaantonov/bluebird

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;
}
  • What is 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.

docs

All 12 comments

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.

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));

@benjamingr - no worries!

Here's the doc

Thanks for the clarification!

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

divramod picture divramod  路  5Comments

icodeforlove picture icodeforlove  路  3Comments

bripkens picture bripkens  路  4Comments

SimonSchick picture SimonSchick  路  6Comments

leahciMic picture leahciMic  路  5Comments