This if is really hard to read. First thing which can be done to reduce code complexity is to check the error conditions first, to manage them, and finally execute the good case (res and res.code == 200) out of the if .