0

I'm trying to check if the result from an api calls returns anything and based on that do some other actions but the following code always go through the process_result() function even though the result is an empty object, can anyone see what I'm doing wrong,

RemoteApi.api_request(server, method, url, data)
    .then(result => {
        debug("result from API request: ", result)
        debug("data from API request: ", data)
        if (result !== {}) {
            debug("in process result now")
            process_result(result, label, model, dispatchOptions)
                .then(body => {
                    onSuccess(body)
                })
                .catch(error => {
                    onFailure(error)
                })
        } else {
            debug("outside process result now")
        }
    })
    .catch(error => onFailure(error))
akano1
  • 40,596
  • 19
  • 54
  • 67

1 Answers1

0

Generally remote requests that return a 2XX result will enter your then() function. Once there its plain old JavaScript.

If you want to ensure the result is an object you can check the type:

if (typeof result === 'object') {
  // success
}

If you don't care for the result contents but you want to check if data is there, you could just check if its truthy:

if (result) {
  // success
}

The remote result could never be an identical instance of {} so your results process will always be truthy.

noetix
  • 4,773
  • 3
  • 26
  • 47