This question already has answers here:
What do multiple arrow functions mean in JavaScript?
(7 answers)
Closed 3 years ago.
I am studying about the React using textbook. There are some codes I cant understand in the book.
const loggerMiddleware = store => next => action => {
}
I know the anonymous function in the javascript.
(a, b, c) => {
}
But, what is this?
store => next => action => {
}
please help me.
It's a higher-order function, i.e. a function that returns a function. It means the same as
store => next => {
return action => { }
}
Related
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 months ago.
I tried to do this, but it doesn't work. Any help is appreciated thanks!
export class OuterClass {
let isEffectiveUrl = (url:string) => {
let tmpRes:boolean
this.http.get(url).subscribe((result) => {
tmpRes = Object.keys(result).length > 0;
});
return tmpRes;
}
}
You're setting isEffectiveUrl to be a function, not the resulting evaluation. One way to do this would create a local variable and then execute your http.get within a function or one of your lifecycle hooks.
e.g.
export class OuterClass {
isEffectiveUrl: boolean = false;
testUrl(url: string): void {
this.http.get(url).subscribe((result) => {
this.isEffectiveUrl = Object.keys(result).length > 0;
});
}
}
Just remember, this is a reactive model. So what's being run in the subscribe block is asynchronous.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
So I have this javascript code. There are 2 nested functions. How can I return a value to funcA() inside funcB() ?
function funcA() {
funcB(() => {
//return funcA inside here
})
}
Is this even possible without doing it like the following?
function funcA() {
let returnValueA;
funcB(() => {
//change returnValueA inside here
})
return returnValueA;
}
In short: No.
A return statement only defines what is returned from the function it is part of.
This question already has answers here:
stop settimeout in recursive function
(7 answers)
Javascript delete a function
(3 answers)
Closed 2 years ago.
const resumeInfinity = () => {
window.speechSynthesis.resume();
const timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
console.log(timeoutResumeInfinity);
}
utterThis.onstart = () => {
resumeInfinity();
};
need the (resumeInfinity) function to stop working after the speechSynthesi
To avoid error messages that resumeInfinity is not a function you shouldn't delete it but set it to an empty function. But as you set defined resumeInfinity as const you can't change that function.
So what you can do is to either change it to:
let resumeInfinity = () => {
// ... your code
}
And then do change it later to an empty function resumeInfinity = () => {}
But you need to keep in mind that if that original function was passed as callback earlier to somewhere else (e.g. like here setTimeout(resumeInfinity, 1000)) that this callback will still refer to the old function.
So a better solution would be to check if the function should still valid to be executed and do an early out otherwise.
const resumeInfinity = () => {
if( /* some check if isn't valid to call that function anymore */ ) {
return
}
window.speechSynthesis.resume();
const timeoutResumeInfinity = setTimeout(resumeInfinity, 1000);
console.log(timeoutResumeInfinity);
}
But all of these above solutions are in fact just a workaround, because if the logic of your application is correct then such a situation should never happen. So the need to do something like this indicates that you more likely need to think about restructuring your code.
If it is just about stopping the timeout then you need to call clearTimeout(timeoutResumeInfinity), and make timeoutResumeInfinity available at the place at which you know that the speechSynthesi finished.
This question already has answers here:
JavaScript: Passing parameters to a callback function
(16 answers)
Closed 4 years ago.
I tried this but it's immediately invokes addClass after opening website :
function onMouseOver(element, event) {
element.addEventListener("mouseover", () => {
event
})
}
onMouseOver(startAnim, addClass(panelLeft, 'hidden-l'));
onMouseOver(startAnim, addClass(panelRight, 'hidden-r'));
"bind" is what you are looking for and below you can see reference of how to use it.
function innerFn(a, b) {
console.log('Inner fn called with params ',a ,b)
}
function outerFn(c, fn) {
console.log('Outer fn called with params ',c)
fn()
}
outerFn('1', innerFn.bind(null, '2', '3'))
This question already has answers here:
What do multiple arrow functions mean in JavaScript?
(7 answers)
Closed 6 years ago.
Can anyone explain what the code is doing here
return () => next => action => {
const callAPI = action[CALL_API];
if (typeof callAPI === 'undefined') {
return next(action);
}
let { endpoint } = callAPI;
const { types, bailout } = callAPI;
It is initially returning a function but I don't get why there are two further fat arrows after the first.
If the arrow function has only one parameter, then the parameter around that is optional. You just need to have enough parenthesis to understand them better.
return () => (next) => (action) => {
it returns a function, which when invoked returns another function which accepts one parameter, next. Now when that function is invoked, it returns another function, which accepts another parameter action.
That code can be rewritten like below,
return function() {
return function(next) {
return function(action) {
It seems that the outer function returns a function with parameter next and that returns another one function with parameter action. That code in the link that you given has not minified, but that seems to be obfuscated.