0

My goal is, if the first request is catching an error, i dont want it to make another request. Basically conditional request.

Action.js

let condition;

        Service.validation(payload).then(() => {
            condition = true;
        }).catch(errorMessage => {
            console.log("1")
            condition = false;
        })

        
        if (!condition) {
            console.log("NOT valid.. closing..")
            return;
        } else {
            console.log("VALID.. Calling another service")
            const promise = AnotherService.anotherRequest(url, payload);
            return promise.then().catch();
        }

Service.js

 async validation(payload) {
            return this.createOrUpdate(payload, "check");
    }

Generic class for crud

 async createOrUpdate(data, subPath = "") {

        try {
            if (data.id) {    
                const response = await this.rc().put(this.PATH + subPath, data);
                return response.data;
            }
            const response = await this.rc().post(this.PATH + subPath, data);
            return response.data;
        } catch (error) {
            throw this.handleError(error);
        }
    }

 rc = (responseType = "json") => {

        return axios.create({
            baseURL: this.RS_REST_URL,
            validateStatus: (status) => (status === 200 || status === 201),
            headers: {
                "Content-Type": CONTENT_TYPE_JSON,
                "Authorization": "Bearer " + localStorage.getItem(STORAGE_KEY_AT)
            },
            responseType: responseType,
            paramsSerializer: (params) => qs.stringify(params, {arrayFormat: "repeat"})
        });
    }

I make a request const response = await this.rc().post gets an error (in Generic class) and thats i want. But, after i catch an error;

if (!condition) {
            console.log("NOT valid.. closing..")

This part does not wait for service to set condition = false

Service.validation(payload).then(() => {
            condition = true;
        })**.catch(errorMessage => {
            condition = false;
        })**

it is an async function but code does not wait for it to finish.

IN THE CONSOLE

"NOT valid.. closing.."

"1"

Am i missing something here?

3
  • your Service.validation is missing a await Commented Jul 31, 2021 at 1:18
  • return await this.createOrUpdate(payload, "check"); does not solve my problem. the inner class has await. Commented Jul 31, 2021 at 1:21
  • jeah but u are using async so the promise will be resolved into a promise Commented Jul 31, 2021 at 1:30

1 Answer 1

1

In Action. You need call await.

async function () => {
  let condition;
  await Service.validation(payload).....
  if (!condition) ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

It solved my problem but there is no need for condition check anymore. I can manage this with try/catch blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.