I have a simple callback example (more like pseudo-code) where values are summed up or multiplied based on the first function parameter.
This is not like a real-life example. Imagine that the task/calculation inside of functions takes some time (like for e.g. calculating the number of stars in the universe). But if we put this aside, the pattern/template with callback looks like this:
let sum = function(a, b) {
return a + b;
}
let mul = function(a, b) {
return a * b;
}
let dne = function(a, b) {
return 'it does not exist';
}
let doCalculation = function(route) {
switch(route) {
case 'sum':
return sum;
case 'mul':
return mul
default:
return dne
}
}
console.log(doCalculation('sum')(3, 4)) //7
Could this be refactored with Promises in a similar way and if it could be done, could it be done simpler.