EcmaScript syntax [duplicate] - javascript

This question already has answers here:
How do I access previous promise results in a .then() chain?
(17 answers)
Closed 4 years ago.
Question related to EcmaScript:
const firstArg = 'myArg';
this._myFunction(firstArg)
.then((asyncResponse) => this._secondMethod(asyncResponse))
.then((asyncSecondResponse, asyncResponse) =>
this._thirdMethod(asyncSecondResponse, asyncResponse))
.then(() => this._lastMethod());
The problem is:
How to pass to the to the _thirdMethod 2 arguments (one from this._secondMethod(asyncResponse)) - that gives one argument and second is just previous asyncResponse) - I don’t want to define it inside _secondMethod(asyncResponse) to return these two arguments, just want to do it in this PIPE that I wrote above
Is there any syntax for it?

You can also nest the second "then" to keep the first asyncResponse in the scope
this._myFunction(firstArg)
.then((asyncResponse) => this._secondMethod(asyncResponse)
.then((asyncSecondResponse) => this._thirdMethod(asyncSecondResponse, asyncResponse)))
.then(() => this._lastMethod());
}

Related

How is this string as a function argument working (no parentheses?!)? [duplicate]

This question already has answers here:
Backticks (`…`) calling a function in JavaScript
(3 answers)
Closed 1 year ago.
Is this a new way to call a function? Whats this even called? Why do this?
const foo = a => console.log(a)
const k = foo`stuff here?` //whaaaaaaa
//output is ["stuff here?"]
they are called tagged template functions. You can read more on how they work and what they do here: MDN

Is there an easier way to compose javascript promises reolution [duplicate]

This question already has answers here:
Wait for multiple promises to finish
(6 answers)
How do I access previous promise results in a .then() chain?
(17 answers)
How to return many Promises and wait for them all before doing other stuff
(6 answers)
Closed 2 years ago.
I have two Promises (let's say p and q) that will retrieve data when resolved, and I want to do something foo that needs the both data. It has a solution as coded below. But is there a more elegant one?
p.then(pData => {
q.then(qData => {
foo(pData,qData);
});
});
I was wondering something like follows (hypothetical function and):
p.and(q).then(dataArr => foo( ...dataArr ));
In the case there's no such way, is there anything in the promises concept that would disallow this construction?

Why can't I return this function using this specific method? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I have a function, which contains a sub function. I need to return a variable from the sub function however I am not sure how (I am new to JS).
I also want to avoid using a global variable, as I have about 15 more functions similar to this.
It shows the error, x is not defined.
return data.rr_vin
^
ReferenceError: data is not defined
I have tried declaring the variable as an attribute of the function, i.e. function.myvar, however I still get the same issue.
function find_num() {
fetch("somefile.json")
.then(res => res.json())
.then(data => {
a = data.numdata.filter(el => el.Process==="Process1").map(o => o.Finish_Time);
b = recent_time(a)
data.x= Math.max(data.numdata.filter(el => el.Finish_Time===b).map(o => o.Shortnum));
console.log(data.x)
return data.x
})
return data.x
}
Ideally I want to call on the find_num() function to return x.

Design pattern ? passing same multiple argument to the functions and function inside [duplicate]

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.

how get return of forEach in angular 2? [duplicate]

This question already has answers here:
Short circuit Array.forEach like calling break
(30 answers)
return value inside foreach
(3 answers)
Closed 5 years ago.
I do not know if the question should be directed to typescript or angular 2
how get return of forEach in angular 2?
I have the following:
let info = this.array.forEach((i, index) => {
.....
.....
return res;
});
console.log(info);
Has returned -> undefined
Does anyone know how to get this return?
Javascript array comes up with inbuilt function called map, filter etc..So you can use map to iterate through values in array.
let info = this.array.map((res,key) => {
//key can also be accessible
return res;
});
console.log(info);

Categories

Resources