MithrilJS / mithril.js

A JavaScript Framework for Building Brilliant Applications
https://mithril.js.org
MIT License
13.99k stars 926 forks source link

Feature request: promise.finally() #680

Closed dgilland closed 9 years ago

dgilland commented 9 years ago

Thoughts on adding a finally() method (or similarly named method) to the Promise API?

My particular use-case is for clean-up code that needs to always run after an m.request() call regardless of anything that occurs further up the chain.

Or is there already an idiomatic Mithril-way to do this?

dgilland commented 9 years ago

@lhorie Any thoughts on including this? If this is something you'd consider adding, I could take a stab at creating a pull request for it.

maranomynet commented 9 years ago

+1

I've worked around this by doing m.sync([ mRequestPromise ]).then( finalCallback ) which doesn't do the same thing, but is close enough in some cases.

adriengibrat commented 9 years ago

@maranomynet your example is not correct, no need of m.sync which is equivalent to Promise.all in ES6.

The method finally() is closer to something like myPromise.then(finalCallback, finalCallback); return myPromise;.

This request is related to:

404 add catch() method

The idea is to be fully compatible with ES6 API.

And also partially to

1 About Promise implementation in mithil

228 add tap() method

and other issues about promises

If Mithril had a Promise A+ compliant implementation (meaning fully async), it would be easyer to have a 100% ES6 compatible API, and thus use Browser ES6 Promise when available.

May be it's time to use an external polyfill for Promise like https://github.com/jakearchibald/es6-promise Browser support start to be quite good http://caniuse.com/#feat=promises

maranomynet commented 9 years ago

As I said, it's a workaround "which doesn't do the same thing". :-) But it provides a quick way to execute a function after a m.request() promise has been fully finished.

franzliedke commented 7 years ago

@lhorie It seems like this is again gone in v1.0?

dead-claudia commented 7 years ago

@franzliedke It's because it now bundles a conditional polyfill for the ES6 native promise instead of exclusively using its own implementation like it did in 0.2. You may want to keep tabs on a related ES proposal, though.