I need to move from one case to another case based on the condition. For example, this is my code:
switch (req.method) {
case 'GET':
alert('GET METHOD');
break;
case 'POST':
alert('POST METHOD');
break;
case 'PUT':
alert('PUT METHOD');
break;
default:
res.end();
}
In the above code, in the POST
case I need to check, for example if(A === B)
, then go to the PUT
case like that. How to do this?
I need to move from one case to another case based on the condition. For example, this is my code:
switch (req.method) {
case 'GET':
alert('GET METHOD');
break;
case 'POST':
alert('POST METHOD');
break;
case 'PUT':
alert('PUT METHOD');
break;
default:
res.end();
}
In the above code, in the POST
case I need to check, for example if(A === B)
, then go to the PUT
case like that. How to do this?
2 Answers
Reset to default 9Make a conditional recursive
function checkMethod(method) {
switch (method) {
case 'GET':
alert('GET METHOD');
break;
case 'POST':
alert('POST METHOD');
checkMethod('PUT'); // here stand the pros of a function
break;
case 'PUT':
alert('PUT METHOD');
break;
default:
res.end();
}
}
You can do that by removing the break;
. If it reaches the end of the POST case, it will continue with the next case if there is no break;
. More Information on switch fallthrough
Example:
switch (req.method) {
case 'GET':
alert('GET METHOD');
break;
case 'POST':
alert('POST METHOD');
if (A === B) {
break;
}
case 'PUT':
alert('PUT METHOD');
break;
default:
res.end();
}