How to make if else inside the promise then?

Good evening, there are
return Promise.all(myRequests)
.then((arrayOfHtml) => arrayOfHtml.map(result => result.data))
.then((arrayOfHtml) => {

then if else

}).catch(function(e){
//error
});


The essence of what I have to if there is a check if element in json array, if no, then else, so if if gives an error, the script immediately jumps to the catch
I have an if else have a variable, say test, I need if everything is OK to assign true otherwise false
August 19th 19 at 23:36
2 answers
August 19th 19 at 23:38
if if gives the error

What is the error that the variable doesn't exist? So check on the existence of another if'ohms.

I got carried away, wrote the mold of a searching artist with a bunch of tests. Not sure I covered all the cases of missing keys, but it seems to work. https://jsfiddle.net/Stalk/cv3h0yrb/
what error to catch flies? - Nigel_Streich commented on August 20th 19 at 00:23
So the fact that I wrote the test, if the error gives even if(someVar.name) if name in json does not exist, then immediately transferred to the catch and does not else - Kylie.Schaefer18 commented on August 19th 19 at 23:41
looks like you have a problem not in name, and in the root someVar. Possible you have this variable is undefined. - Nigel_Streich commented on August 19th 19 at 23:44
: There actually is a way around this promiseResult[1].someArtist.lastflm.artist.img[0]

Let's say it comes to the artist and love, and he fails and immediately jumps to the catch and as much as I did not write that to something like if(json(which is higher) === 'underfind') and so on, nothing helps - Kylie.Schaefer18 commented on August 19th 19 at 23:47
: a possible example of the data? Or that tvy there from lastfm request? - wilma_Gra commented on August 19th 19 at 23:50
:
it comes to the artist and love, and he fails and immediately jumps to the catch

I say the same - it seems that you have in the chain the picture(img) undefined, and the variable artist(or any the parent object higher in the chain).
View that the object 'e' in the catch block of the promise tells you... - Nigel_Streich commented on August 19th 19 at 23:53
by the way, according to the chain there is a possibility that img is really undefined and you're trying to call operator [ ]. In this case also thrown out acceptn and control passes to the catch block. - Nigel_Streich commented on August 19th 19 at 23:56
: Exactly, I need to catch when the img is not present, because sometimes it happens that no such artist on last FM or no pictures, but in this case I need to make a variable inside of if var some = false

But he didn't pass to catch up and do the function on it by using some variable - Kylie.Schaefer18 commented on August 19th 19 at 23:59
: Can you give an example of successful and unsuccessful queries? - wilma_Gra commented on August 20th 19 at 00:02
: Added in the post above a link to an example with a bunch of different checks. Also, instead of the empty string you can substitute stub by default. - wilma_Gra commented on August 20th 19 at 00:05
: so first check the existence of the artist, and then, if it exists - check img
something like this:
if (promiseResult[1].someArtist.lastflm.artist && promiseResult[1].someArtist.lastflm.artist.img)
do_something();
else
do_else(); - Nigel_Streich commented on August 20th 19 at 00:08
But to get essentially the same thing if he comes to the point where there is error, it enters a catch I did the test in an if - Kylie.Schaefer18 commented on August 20th 19 at 00:11
In your example if you remove all and leave only the data[0].artist and provide an incorrect query, then it moves to the else and I ignore it - Kylie.Schaefer18 commented on August 20th 19 at 00:14
: : https://toster.ru/answer?answer_id=957603#answers_... - Kylie.Schaefer18 commented on August 20th 19 at 00:17
: Here's how I https://jsfiddle.net/cv3h0yrb/1/ - Kylie.Schaefer18 commented on August 20th 19 at 00:20
: Do you inspect the existence of
if (arrayOfHtml[0].results.artistmatches.artist[0]['image'][2]['#text']) { /* ... */ }
but if "open" somewhere before, for example, missing image, naturally there is a bug.
Can be rewritten as:
if (arrayOfHtml 
&& arrayOfHtml[0]
&& arrayOfHtml[0].results
&& arrayOfHtml[0].results.artistmatches
&& arrayOfHtml[0].results.artistmatches.artist
&& arrayOfHtml[0].results.artistmatches.artist[0]
&& arrayOfHtml[0].results.artistmatches.artist[0]['image']
&& arrayOfHtml[0].results.artistmatches.artist[0]['image'][2]
&& arrayOfHtml[0].results.artistmatches.artist[0]['image'][2]['#text']) {
// ...
}

If you think it's somehow wrong, well, this situation probably arose because the above code no tests were not. If there was - you can check the entire chain.
You can write a separate function that will be held throughout the chain
https://jsfiddle.net/Stalk/cv3h0yrb/3/ (see f-tion has()) - wilma_Gra commented on August 20th 19 at 00:26
August 19th 19 at 23:40
return Promise.all(myRequests)
.then((arrayOfHtml) => arrayOfHtml.map(result => result.data))
.then((arrayOfHtml) => {
 //Try to get a picture from the last FM, if no, give an empty string
if(arrayOfHtml[0].results.artistmatches.artist[0]['image'][2]['#text']){
 last = arrayOfHtml[0].results.artistmatches.artist[0]['image'][2]['#text'];
 }else {
 last = false;
}

 if(last) {
some();
}

})
.catch(function(e){
 //Catch error 
 console.log(chalk.red("Error" +e));
 });

Find more questions by tags Node.js