Domanda

I want to create a code block within Bluebird that is safe and has access to progress() - is this possible:

One way of doing what I want is with exception handlers - but I'm looking for a solution without:

var deferred = Promise.defer();
try {
    ...
    deferred.progress(10);
    ...
} catch(e) {
    deferred.reject(e);
} 
return deferred.promise;

Also according to the documentation:

The use of Promise.defer is discouraged - it is much more awkward and error-prone than using new Promise.

So I can do this:

return new Promise((resolve, reject) => {
    ...
});

But I have no way of progressing that one?

Thanks,

È stato utile?

Soluzione

Promise.method can be used to make any function safe

var fn = Promise.method(function() {

    var deferred = Promise.defer();
    deferred.progress(10);
    return deferred.promise;

});

Altri suggerimenti

You're right, the new Promise constructor does not provide a way to fire progress events. The reason is probably the ongoing discussion about the promise construction spec, where none of the proposed methods has been approved yet.

You can check what the PromiseResolver .progress() method does: you just would need to call the "private", undocumented ._progress() method on the promise - but that is not advisable.

@Esailija

Thanks, almost what I ended up with ....

return new Promise((resolve, reject) => {
    var deferred = Promise.defer();
    ....
    deferred.progress(10);
    ....
    resolve(deferred.promise);
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top