Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
@Cyanide finally someone come to the rescue....
I wrote this
```javascript
export default class A
{
async foo(a, b)
{
return 'something';
}
async bar(c, d)
{
this.foo(c, d);
}
}
```
and go an "undefined" error. So in my case how to I call a function within the same class?
I tried remove the `this` still return the same result -
dmonkey23273y
-
@dmonkey actually that function would return something else after assign into another function .
-
joas19423yAre you trying to return an object in foo and accessing it's property gives you undefined? Async functions return the value wrapped in a Promise. To wait for it to resolve in other async function you can use await keyword.
-
1. You may have forgotten to add a return statement in bar
2. When you call an async function, either await it or make the callee sync and return the resulting promise. It's technically legal to return a promise from an async function but it's better to be clear in your intentions. Always either await or return promises, or reflect in a comment on why you chose not to. Unhandled promise rejections are terrible, not to mention they crash Node.
3. If you're new to JS, remember that this will not work:
```
const a = new A()
const myfunction = a.bar
myfunction(1, 2)
```
This will fail due to how `this` works in JS, MDN has a really good article about it. It's not complex at all, but it's not very convenient which is why I make an effort to avoid classes and OOP altogether and use factories and composition instead. -
JavaScript developers are angry that they open their eyes and see JavaScript all day. The poor things.
Also, you're a bitch. (I'm a JS dev) -
Angry15703yYou must not be asking in the right community. The one I've joined just ignores you XD
-
@Angry well, that's equally sad. Is it me, or collective silence can also be offending?
-
@Angry try the one in telegram.... They will scold you and pick a fight with you ... They will say "how you don't know this ? Are you stupid ..." Something like that ...every JavaScript group I joined behave this way idk why.
But luckily in my discord I found a server name coding help. They seems ok technically solve my doubt. Not to mention people in Devrant helped me too. -
john-doe9403y@johnmelodyme well, it is somehow expected that you read a good book before asking questions in some places.
* JavaScript the good parts
* JavaScript Allongé
Are 2 good oldies that tackle some of the most annoying parts is JS by focusing on what makes JS great.
The most confusing things, like the function binding problem that was mentioned earlier will be covered in a good book, also the different rules for hoisting, scoping and closures.
The ones I mentioned would probably not be up to date with the classes and async/await grammars, but those are kind of new and pretty much covered by any decent article online.
Also if you're into typescript, the TS language reference is pretty good once you've got the basics and the quirks of JS. -
john-doe9403y@johnmelodyme
* JavaScript the good parts
* Javascript Allongé (I think this one is free to read online)
Seiously though , Javascript Developer community are toxic shit! All I did was asked a question regarding the syntax.
They started to curse me and call me names , like what the fuck? I just want to know some syntax. Dont have to be an arse.
At least Elixir community is polite.
rant
fuckjsdev