How can I early exit from a function in my TypeScript file?
checkIfFollowed(){
this.currentUserInfos.followed.forEach(element => {
if(18785 == 18785){
console.log('its true');
this.alreadyFollowed = true;
return; // Exit checkIfFollowed() here
}
});
this.alreadyFollowed = false;
console.log('the end');
return;
}
When I'm running it, it's executed completely, but it should exit after the first:
'its true'
But in my console, I get:
its true
its true
the end
Foreach loops 2 times as expected, but why the method doesn't stop after hitting a "return"?
I'm not trying to get out of the forEach, but ending the method 'checkIfFollowed' in the foreach.
'the end' should not be printed.