question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat(finally): finally should receive as parameter the state of the promise

See original GitHub issue

As per https://github.com/angular/angular.js/issues/9246 , it would be nice and more developer friendly to be able to receive the state of the promise inside the finally call.

My suggestion is for .fin() to receive 2 parameters:

  1. a boolean – if the promise was resolved (TRUE) or rejected (FALSE)
  2. a mixed – value sent to .resolve() or .reject()

@kriskowal : what do you think?

I’ll post here the real use-case again. We have a SPA and we are using AngularJS as a framework. I know Q service is a bit different than AngularJS $q, but the logic seems to be very similar.

Our use-case is when all the ajax requests (error or success) may contain a set of messages - those messages should be automatically pushed to the messages component and be shown. The problem is that we have to write:

.then(
  function (data) {
    msgService.notify(data);
  },
  function (data) {
    msgService.notify(data);
  }
);


My suggestion was to have something like:
.then(...)
.finally(function (isResolved, data) {
    msgService.notify(data);
});

We can even adapt our notify and just put “.finally(msgService.notify)”.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:32 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thatshaileshcommented, Feb 6, 2018

@domenic How would you implement something like

Promise.reject(new Error("boo!"))
  .catch(function () { console.log('it should not go to next promise') })
  .then(function () { console.log("I should be called only if there is no error"); });

Below code doesn’t work since .finally() doesn’t take any arguments

.then((something) => {
  return () => { something.log(...) }
})
.catch(next)
.finally(cb => {
  //check if cb === function
  cb.call(this)
})

One way to do that is

Promise.reject(new Error("boo!"))
  .catch(function (err) { next(err); 
     return {notify: false}
 })
.then(res => {
  if (res.notify) { console.log("I should be called only if there is no error"); }}
})

Any other cleaner way to achieve the same?

0reactions
benjamingrcommented, Feb 7, 2018

That train has sailed by now - finally is part of JavaScript.

Also, you should just do:

Promise.reject(new Error(“boo!”)) .then(function () { console.log(“I should be called only if there is no error”); }, function () { console.log(‘it should not go to next promise’) });

Or, for this behavior do what synchronous code would (and rethrow the error with a throw).

Also, it is considered bad etiquette to bump issues from 2014

Read more comments on GitHub >

github_iconTop Results From Across the Web

Promise.prototype.finally() - JavaScript - MDN Web Docs
The finally() method of a Promise object schedules a function to be called when the promise is settled (either fulfilled or rejected).
Read more >
JavaScript Promise finally() Method - GeeksforGeeks
The finally() method of the Promise object is used to return a Promise when a Promise is settled, that is, it is either...
Read more >
How do I access previous promise results in a .then() chain?
These state objects accumulate the results of the previous actions, handing down all values that will be needed later again plus the result...
Read more >
JavaScript Promise finally()
Summary: in this tutorial, you will learn how to use the JavaScript Promise finally() method to execute the code once the promise is...
Read more >
Delivering on the Promise
Finally, this report was made possible by support from the Ewing Marion Kauffman Foundation, ... collect for their oversight of charter schools is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found