0

I've this quick doubt that might be related to promise chaining but I'm not able to think about it clearly.

So basically, I've this function(myFn) that returns axios.request() which basically returns a promise, so i can now do myFn.then().catch() and it works, now what I want to do is to create a promise wrapper on top of that, which will resolve only when response.data.status =="somevalue", here response.data means the request was successful and a result came but now i want to resolve myFn only when response.data.status =="somevalue".

Aman
  • 61
  • 1
  • 3
  • 2
    Give a [mre], not just a vague description of your code. What are you currently doing in the `.then` callback if **not** checking the condition you want? – jonrsharpe Aug 15 '22 at 19:42
  • Hey, Yes i can check response.data.status in .then() and do whatever i want to achieve but basically i have a ton of similer api endpoints and i want to make sure i do anything only when response.data.status===true, everything else goes in error, like i want to reject if response.data.status==="false". So i want to create a wrapper for the same. I know this isn't the best practice but this is perhaps what i want to achieve right now. Thankyou – Aman Aug 15 '22 at 19:48
  • @Aman Please update the question instead of adding code to comments. Your question should ALWAYS contain code. – Ruan Mendes Aug 15 '22 at 19:49

2 Answers2

3

Since your function returns a promise, you can just use the promise's result in the .then callback. If the response.data.status isn't what you want, throw an error, otherwise return the response, thus resolving the promise:

myFn().then(response => {
  if (response.data.status !== "somevalue") {
    throw new Error("invalid status");
  }
  return response;
})

Edit: If you want to wrap this in a custom Promise, you could do something like this:

const myPromise = new Promise((resolve, reject)=>{
    myFn().then(response => {
    if (response.data.status !== "somevalue") {
        reject("Invalid status");}
    else{
        resolve("somevalue");}
    });
});

And then call this promise with callback functions for the fulfilled and rejected cases of the promise:

myPromise.then(handleSucess, handleFailure);

For reference see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

  • Did you mean to say that you should create a function that returns the code you have shown? `function myWrapperFn() {return myFn.then(...)}` – Ruan Mendes Aug 15 '22 at 19:55
  • If you want a wrapper that works for any myFn function (which returns a Promise) then yes, your myWrapperFn() would suffice. Your original question detailing a "promise wrapper" seems slightly misinformed because you don't need the myWrapperFn() to be another Promise. Once the axios request fulfills and is returned by myFn(), you'll know whether the status was somevalue or not and can check it accordingly. Thus there's nothing else to wait on, so no additional Promise is necessary. – Braxton Little Aug 15 '22 at 20:11
  • I think the OP is saying they don't want to add that check in every place they call `myFn` – Ruan Mendes Aug 15 '22 at 20:23
  • That's what a wrapper accomplishes; minimizes repetition of code. If the OP is calling myFn multiple times, they'll want a wrapper function that checks the promise's response instead of checking it's response within every function call. – Braxton Little Aug 15 '22 at 20:31
  • Hi, Yes i think it'd work but what if i don't want to throw an error but rather i want to create a new promise and then `resolve()` or `reject()` based on the response of `axios` promise, and continue the statement. I want a promise way of doing this. I mean when i can return `new Promise` based on a custom logic like say Boolean `if(true){resolve()}`, then why not here? – Aman Aug 16 '22 at 10:23
1

You can try if this works for you. I am not sure though, if the throw here works as expected.

const axiosInstance = axios.create({
    transformResponse: [function (data) {
        if (data.status === "somevalue") {
            return data;
        }
        throw new Error(`wrong status: ${data.status}`);
    }],
});

Then use this axiosInstance to do your calls.

If this does not work, you could also give the axios response interceptors a try. How can you use axios interceptors.

HEllRZA
  • 287
  • 1
  • 7