0

I wonder if what I'm doing is in the right way What I want to do is create a Promise with a customized ".then ()" ex: myPromise.accepted (value => console.log (value))

what i managed to do

Promise.prototype.accepted = Promise.prototype.then

js code for this

const myPromise = new Promise ((resolve, reject) => {
     resolve (true)
})

myPromise.accepted (status => console.log (status))

but I don’t know if it’s the right way to do it

Ashish Kamble
  • 2,555
  • 3
  • 21
  • 29
Pedro Henrique
  • 117
  • 1
  • 3
  • 3
    **Why** would you want to do this. Everyone know what `then()` means, they can look it up if they don't. Looking at your code and seeing `somePromise.accepted(...)` is super confusing, and it's hard to see the benefit. What **is** the benefit over just using `then()`? – Mark Mar 02 '21 at 03:25
  • The advantage I see at the moment is more for learning, and also to make it easier to understand what will happen – Pedro Henrique Mar 02 '21 at 03:27
  • Take a look of this link and you'll find a good solution and explanation why is not recommended. https://stackoverflow.com/a/44376210/2566313 – marcode_ely Mar 02 '21 at 03:32
  • It's *a* right way to do a wrong thing. This makes it *harder* to understand what will happen because you are the only person in the world that will do this. Plus "accepted" has a real-life meaning that may or may not apply to what is actually happening in the code. – Dave Newton Mar 02 '21 at 03:35
  • This will make it harder for others to read your code. It will also slow you down from learning how to read other people's code so this is bad for learning to understand javascript. Unless you do not plan to write code in the future (you are only learning coding for fun and don't plan on writing code as a career) the majority of your time coding will be reading other people's code, not your own code. – slebetman Mar 02 '21 at 03:39
  • It hard to know what you're asking: creating an _alias_ for a method is not the same as _customizing_ a method as, for example, making it have different or more side effects than the original. As others have said, creating an alias for `.then` would be non standard. – traktor Mar 02 '21 at 04:08
  • Ignoring the 'omg don't do this' comments for a bit. What problem did you have with your code sample? it looks reasonable – Evert Mar 02 '21 at 06:49
  • I went to sleep yesterday, I appreciate all the comments, I will not use it that way, I understand that this is a bad practice – Pedro Henrique Mar 02 '21 at 12:16

0 Answers0