There are 3 functions imported:
import { searchShip, searchLcl, searchFcl } from "services/logisticsService";
searchData = {
currentPort: currentPort,
destinationPort: destinationPort,
date: readyTOLoad,
type: containerType, // containerType is geting 1 value among this (ship,lcl,fcl)
}
Here I want to call one of those function based on that containerType value. For example:
If I get type value as ship then it should be
searchShip(searchData).then((response) => {
console.log(response)
})
If I get type value as lcl then it should be
searchLcl(searchData).then((response) => {
console.log(response)
})
If I get type value as fcl then it should be
searchFcl(searchData).then((response) => {
console.log(response)
})
Is there any good way to call that search function on that type condition without if else statement because inside it there is very lengthy process to handle response.
Yes, you can define an object that has a {[key]: function} structure.
const handlersByType = {
ship: searchShip,
lcl: searchLcl,
fcl: searchFcl.
};
handlersByType[type](searchData).then((response) => {
console.log(response)
})