Let's imagine you have a number of separate functions that accept a string variable as an argument and return it with some modifications for further use. What is the most simple and elegant pattern to chain the function calls around a single variable in vanilla JavaScript you prefer and recommend to use in production?
-
Function sandwich aka nested madness (straightforward, but not nice, poor readability):
let str = 'random text'; str = func4(func3(func2(func1(str))));
-
Copy-paste method (simple, readable, but repetitive):
let str = 'random text'; str = func1(str); str = func2(str); str = func3(str); str = func4(str);
-
Array juggling (feels goods and automated, but not super neat):
let str = 'random text'; [func1, func2, func3, func4].forEach((func) => { str = func(str); });
-
Promisification (looks clean, but has async side-effects):
let str = 'random text'; str = await Promise.resolve(str) .then(func1) .then(func2) .then(func3) .then(func4);
Could you please suggest other fancy ways? I stick to #1, but not sure if it's good enough.
Aucun commentaire:
Enregistrer un commentaire