0

What would be the best way to check if a condition matches any value in array?

So for example I want to implement a retry logic if there is a 5xx error received.

  var searchUserRequest = httpClient.request(searchUserRequestOptions, (res => {
    if(res.statusCode ===  500 || res.statusCode ===  501 || res.statusCode ===  502) {
      process.stdout.write('HELLO!!!!!! ');

      }

  }));

ideally I would like to put all the 5xx error does in a list or an array and then check if the res.status code equals any of the 5xx error codes.

What is the best way to do this in Javascript? Can I do this without a loop?

sultania23
  • 322
  • 3
  • 11
user6248190
  • 1,233
  • 2
  • 16
  • 31

3 Answers3

4

Why not just check to see if the statusCode is between 500 and 511 (that looks to be the range)? An array is overkill.

var searchUserRequest = httpClient.request(searchUserRequestOptions, (res => {
  if(res.statusCode >= 500 && res.statusCode <= 511) {
    process.stdout.write('HELLO!!!!!! ');
  }
}));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

ideally I would like to put all the 5xx error does in a list or an array and then check if the res.status code equals any of the 5xx error codes.

You can convert the statusCode to string and check if it starts with 5

var searchUserRequest = httpClient.request(searchUserRequestOptions, (res => {
  if (res.statusCode.toString().startsWith('5')) {
    process.stdout.write('HELLO!!!!!! ');

  }
}));

Alternatively you can also use aobject literal to store statusCode as code and a corresponding message

let statusCodes = {
  501: 'someMsg for 501',
  502: 'someMsg for 502',
  503: 'someMsg for 503',
}


var searchUserRequest = httpClient.request(searchUserRequestOptions, (res => {
  if (statusCodes[res.statusCode]) {
    process.stdout.write('HELLO!!!!!! ');

  }
}));

Another option is to keep all the status codes in a Map

var statusCodesMap = new Map();
statusCodesMap.set(501, "value associated with 'a 501'");
statusCodesMap.set(502, 'value associated with 502');
statusCodesMap.set(503, 'value associated with 503');

var searchUserRequest = httpClient.request(searchUserRequestOptions, (res => {
  if (statusCodesMap.get(res.statusCode)) {
    process.stdout.write('HELLO!!!!!! ');
  }
}));
brk
  • 48,835
  • 10
  • 56
  • 78
0

If you need ES5 compatible code you could use Array#indexOf (returns the index of the element in the array or -1 if the element does not exist in the array) for example:

var searchUserRequest = httpClient.request(searchUserRequestOptions, (res => {
    if([500, 501, 502].indexOf(res.statusCode) !== -1) {
        process.stdout.write('HELLO!!!!!! ');
    }
}));

If you can use ES7 you could use Array#includes:

var searchUserRequest = httpClient.request(searchUserRequestOptions, (res => {
    if([500, 501, 502].includes(res.statusCode)) {
        process.stdout.write('HELLO!!!!!! ');
    }
}));

If you are certain that will not need to check for any values that are in the 500s you could just use 500<= statusCode && statusCode < 600 as CertainPerformance suggested or use regular expressions: /^5\d{2}$/.test(statusCode)

nick zoum
  • 7,216
  • 7
  • 36
  • 80