javascript - Cancel a delayed Bluebird promise -


how reject delayed promise:

const removedelay = promise.delay(5000).then(() => {     removesomething(); });  //undo event - if invoked before 5000 ms, undo deleting removedelay.reject(); // reject not method 

bluebird v3

we no longer need declare promise 'cancellable' (documentation):

no setup code required make cancellation work

simply call cancel on promise:

const promise = new promise(function (_, _, oncancel) {     oncancel(function () {         console.log("promise cancelled");     }); });  promise.cancel(); //=> "promise cancelled" 

as may have noticed, cancel method no longer accepts reason cancellation argument. logic required on cancellation can declared within function given oncancel, third argument given promise constructor executor. or within finally callback, not considered error when promise cancelled.

revised example:

const removedelay = promise     .delay(5000)     .then(() => removesomething());  removedelay.cancel(); 

______

pre bluebird v3

take @ documentation promise#cancellable:

by default, promise not cancellable. promise can marked cancellable .cancellable(). cancellable promise can cancelled if it's not resolved. cancelling promise propagates farthest cancellable ancestor of target promise still pending, , rejects promise given reason, or cancellationerror default.

we can use so:

const removedelay = promise     .delay(5000)     .cancellable() // declare promise 'cancellable'     .then(() => removesomething());     .catch(err => console.log(err)); // => 'reason cancel'  removedelay.cancel('reason cancel'); 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -