Sunday 26 December 2021

Promises and catch

I've recently been working on JavaScript code written by other people where they were using .then()/.catch() chains rathern than async-await magic, and I should not change that, so I've had to review a bit how then()/.catch() works (thanks to async-await I've rarely used them in the last years).

The first point is a reminder about promises rejection in general. When inside .then() we want to reject the promise, should we use Promise.reject(new Error()) or throw new Error()?
Well, it's basically the same. If you do a throw inside a .then or a .catch the .then or .catch will trap the exception and reject the current promise. You can read more about this here.

Both methods produce the exact same response. The .then() handler catches the thrown exception and turns it into a rejected promise automatically.

The second point is that I had almost forgottent that the .then() method accepts a second parameter, a handler function to be called when the promise has been rejected. So, what's the difference between that and chaining a .catch() call? I mean:



getData()
.then(
	data => {/* do whatever */},
	error => {/* treat error */}
);

vs

getData()
.then(data => {/* do whatever */})
.catch(error => {/* treat error */});



Well, the behaviour is almost the same, but there's a subtle difference. If the success handler throws an error, in the first case, that error is not being caught, but in the second case, the .catch, will catch that error. It's well explained here

The next thing is with how many .catch() calls you need. If you have a chain of .then() calls and want to catch any exception in any of them, and apply the same treatment, you need just one .catch() at the end of the call. This means that this code:


async function getProcessedMessage(){
	try{
		let msg = await getMessage();
		msg = await correctOrtography(msg);
		msg = await replaceOddWords(msg);
		msg = await addExtraFormatting(msg);
		return msg;
	}
	catch(error){
		console.log("unable to perform the whole processing");
		return "";
	}
}

is equivalent to this:


function getProcessedMessage(){
	return getMessage()
	.then(correctOrtography)
	.then(replaceOddWords)
	.then(addExtraFormatting)
	.catch(error => {
		console.log("unable to perform the whole processing");
		return "";
	})
}


If what we want is managing exceptions call by call (I mean, differently for different .then() calls), to do some treatment (logging the exception and throwing a "higher level one") or recovering (we replace by a default value and continue with the ensuing calls) then we'll have a .catch() for each .then(). This code:


async function getProcessedMessage(){
	let msg;
	try{
		msg = await getMessage();
	}
	catch(error){
		console.log("failed to retrieve message, we'll use the default");
		msg = "default";
	}
	try {
		msg = await translate(msg);
	}
	catch(error){
		console.log("low level error: " + error.message);
		throw new Error("failed to translate);		
	}
	return msg;
}

is equivalent to this:


function getProcessedMessage(){
	getMessage().
	.catch(error){
		console.log("failed to retrieve message, we'll use the default");
		return "default";
	}
	.then(translate)
	.catch(error){
		console.log("low level error: " + error.message);
		throw new Error("failed to translate);			
	}
}

Thursday 23 December 2021

FIC Xixón 2019

Since I started this blog, for every edition of the Xixon Internationl Film Festival (FICXixon) during which I was in town, I've done a review of the screenings that I attended to. When starting to write the one for 2021 I've realised that I had missed to complete and publish the one from 2019 (in 2020 due to the pandemic the festival was only online, and I did not watch any film). So well, 2 years late, here it is!

Though in 2019 I was in Xixón during the 9 days of the festival, I only attended 3 films of this 57th edition. After the years that I missed the festival cause I was living abroad I no longer have that urge to attend 1 (or more) films per day, and mainly, I only look into films being screened in Teatro Jovellanos, the main site for the festival. Also, I missed a couple of films cause I did not hurry enough to get the tickets... This said, these are the films I watched

  • Thursday, 2019/11/21, La hija de un Ladrón, Belén Funes, Spain, 2019 A good film. It goes through the life of a young Spanish girl striving to get a life for for her and her little kid. She has not had an easy life, with a missing mother; a father that has spent time in prison, an odd relation with the father of her child... and lives for the moment in a shared flat managed by the Catalan social aid system (she's not part of any 'minority' and does not seem to be into drugs or have committed any crime, so don't expect she'll be considered of particular interest to receive much help from the social system...) The girl is a fighter, doing her best to get and keep a job, and to protect her kid and her little brother. Recorded in a rather realistic style, probably this is not a film that you will particularly remember, but you won't quit it either, and you'll have the feeling of having spent your time correctly.

  • Friday, 2019/11/22, Rounds, by Stephan Komandarev, Boulgarie, Serbie, France, 2019. I think this is the second Bulgarian film that I've watched in my life (at least that I'm aware of) and as my previous Bulgarian experience, this is an excellent work, really excellent. It follows the lives of 3 pairs of Sofia policemen working the night shift, and combines masterfully black humor and dark, brutal events to portrait the harsh reallity that those living in that souther slav country face day after day. You can read more here.

  • Saturday, 2019/11/23, Synonyms, Nadav Lapid, Israel-Germany-France, 2018. An absolute piece of crap. There's nothing in this film that is worth wasting your time in front of it.