This question already has answers here:
What do multiple arrow functions mean in JavaScript?
(7 answers)
Closed 12 months ago.
Why do we have => twice. Not able to understand the arrow function declaration.
const asyncFunctionMiddleware = storeAPI => next => action => {
// If the "action" is actually a function instead...
if (typeof action === 'function') {
// then call the function and pass `dispatch` and `getState` as arguments
return action(storeAPI.dispatch, storeAPI.getState)
}
// Otherwise, it's a normal action - send it onwards
return next(action)
}
asyncFunctionMiddleware takes one argument storeAPI and returns an unnamed function. This unnamed function takes one argument next and returns another unnamed function. This unnamed function takes one argument action and returns a value.
Related
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 9 months ago.
I want to give the anonymous function's name that is inside my function based on the parameters passed. For example, if the passed parameter is "nature" then I want to create an anonymous function with that name.
Example code:
function hello(nature) {
window.nature /* I want this word "nature" to be taken from the parameter passed by the function */ = function () {
console.log('succes');
}
}
You can do it by passing a string as parameter.
Here how to assign a custom name and a custom function, if needed.
function hello(name, func) {
window[name] = func;
}
hello('azerty', () => console.log('hello'));
window.azerty() // output 'hello'
Here is an exemple where variable "nature" has value "fn" and parameter passed to "fn" is logged to the console.
function hello(method) {
window[method] = function (param) {
console.log(param);
};
}
hello('fn');
window.fn('success');
This question already has answers here:
Resolve promises one after another (i.e. in sequence)?
(36 answers)
How to execute promises sequentially, passing the parameters from an array?
(9 answers)
How to chain execution of array of functions when every function returns deferred.promise?
(7 answers)
Closed 2 years ago.
PromisesInSeries function that takes an array of asynchronous functions and sequentially (the next one starts when the previous one has finished) calls them, passing the result of calling the previous function as arguments
function promisesInSeries(asyncFns) {
let result;
return new Promise((resolve, reject) => {
for(const fn of asyncFns){
resolve(fn)
.then(data => fn(data))
}
})
}
I only get the results of the first function. How to call all functions from an array and, as a result, return the last value?
Without async/await you could reduce the fns array:
function promisesInSeries(fns) {
return fns.reduce((promise, fn) => promise.then(fn), Promise.resolve());
}
The first fn will receive the value undefined, after that each return value is passed to the next function.
If you want to have some initial value, you can provide it by changing Promise.resolve() to Promise.resolve(initialData). Where initialData could be a static value or a value passed through function parameters.
If you can use async-await, this is way easier:
async function promisesInSeries(asyncFns) {
let result;
for (const fn of asyncFns) {
result = await fn(result);
}
}
This question already has answers here:
Two sets of parentheses after function call
(4 answers)
Closed 4 years ago.
const List = connect(mapStateToProps)(connectedList);
There is a function called "connect" being called with "mapStateToProps" as the argument, however there is "connectedList" surrounded by parenthesis right after the function calling. I haven't seen that before and I didn't find anything about that in the es6 articles I read for research.
The connect function most likely returns another function that accepts one argument which is being invoked.
function getFunc(arg){
alert(arg);
return function(arg2){
alert(arg2);
}
}
getFunc("arg1")("arg2");//invokes the getFunc function and invokes the returned function
This question already has answers here:
How do JavaScript closures work?
(86 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 5 years ago.
I need to make some kind of form item factory with javascript. The idea is that you get the data from backend and produce the forms which need to have different event handler which is taking same argument but different contents.
const updateHandler = (dispatch, formName, section) => {
return (value) => {
//do some stuff with value
//... this part is different for the each different form
//call function that handles updating redux and url
updateUrl(dispatch, formName, section, value, ...)
//taking same argument
}
}
I am wondering if I can memorize 3 shared argument for sub function so that i do not have to pass same thing again.
I thought about currying or apply but i do not i can achieve that with currying or apply.
This question already has answers here:
Passing arguments forward to another javascript function
(5 answers)
Closed 9 years ago.
I have a method defined, say abc, which accepts a function name, say xyz and the arguments to that function as its arguments. Now, the abc method needs to call xyz with the arguments received.
xyz can be any method and could be having any number of arguments, also, xyz cannot be using arguments keyword. rather, the arguments for the method should be passed as separate variables.
Ex: xyz will be defined as:
var xyz = function(a,b){//code for processing a and b}
I am calling the abc method with the first argument as the function to be invoked and rest of the arguments as arguments to be passed to the function while calling it. In abc, I am making use of arguments keyword to access the dynamic number of arguments sent. But from there I am not sure how to call the next function with the arguments.
Use the apply method of functions to call it with a variable argument list:
function abc(funcName) {
funcName.apply(null, [].slice.call(arguments, 1));
}
You could also pass along your current context by supplying the first argument:
function abc(funcName) {
funcName.apply(this, [].slice.call(arguments, 1));
}