1

I have this function

function deleteItem() {
   deleteInvItems({
        where: {
            invoice_id: invData.invNo,
            [Op.not]: [{
                item_id: itemIDs
            }]
        }
    }).then(resp => {      
        if (!resp) return false;
    });
    
    deleteInvSumItems([{
        where: {
            invoice_id: invData.invNo,              
            [Op.not]: [{
                hsn_code: invUniqueHsnCodes
            }]
        }
    }]).then(resp => {      
        if (!resp) return false;
    });
    
    return true;
}

I want to exit the function with return false; if I get false response from server.

But if I call inner functions without return keyword unlike return deleteInvItems({...}) and return deleteInvSumItems({...}). I'm not able to exit the parent function.

How can I exit the parent function deleteItem() with retuning false if I get false response from server?

user15557745
  • 139
  • 6
  • 1
    [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321) – adiga Apr 24 '21 at 08:56
  • Where does ```resp``` come from and what do you mean with exit the parent function? What happens - infinite loop or an error? – biberman Apr 24 '21 at 09:20
  • Are you looking to only run deleteInvSumItems if deleteInvItems doesn't fail? i.e. is the purpose of the `if (!resp) return false;` in the then() callback of deleteInvItems to stop deleteInvSumItems from running? – Ben Stephens Apr 24 '21 at 09:33

5 Answers5

1

You might want to use the async/await syntax instead of Promise.then(...).

async function deleteItem() {
  try {
    const response = await deleteInvSumItems(...)    // pass parameters here
    if(!response) {
      // since this is not part of an inner callback function
      // it will exit the deleteItem function
      return       
     }
   } catch (err) {
     // handle error
   }

Note: async/await is supported since ECMAScript 2017, so make sure your platform where you run your code supports it.

zunnzunn
  • 366
  • 1
  • 5
  • 12
0

Your function needs to be async:

async function deleteItem() {
   const res = await deleteInvItems({
        where: {
            invoice_id: invData.invNo,
            [Op.not]: [{
                item_id: itemIDs
            }]
        }
    })
// do something with res
}
PixAff
  • 309
  • 4
  • 14
0

Since your code is asynchronous, you can't "just" exit the function.

You have to work with Promise, or with Async/Await.

If you choose to work with promises, just return the promise, and use then.

I think that since your code needs to stop the then chain with 2 promises (deleteInvItems and deleteInvSumItems), it's easier and readable to use async await here:

//when you call the function, use await to get the result:
let finalResult = await deleteItem();

async function deleteItem() {
  let val1 = await deleteInvItems({
    where: {
      invoice_id: invData.invNo,
      [Op.not]: [{
        item_id: itemIDs
      }]
    }
  }).then(resp => {
    if (!resp) return false;
  });
  let val2 = await deleteInvSumItems([{
    where: {
      invoice_id: invData.invNo,
      [Op.not]: [{
        hsn_code: invUniqueHsnCodes
      }]
    }
  }]).then(resp => {
    if (!resp) return false;
  });

  return val1 && val2;
}
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35
-1

Use the break statement with a label reference, to "jump out" of a nested for loop:

 let text = "";
 let i;
     mainLoop:
   for (let k = 0; k < 5; k++) {
     for (i = 0; i < 5; i++) {
       if (i === 3) {
         break mainLoop;
       }
       text += "The number is " + i + "<br>";
     }
   }
 document.getElementById("demo").innerHTML = text;
<p id="demo"></p>
Thiwanka Wickramage
  • 782
  • 1
  • 10
  • 22
-3

You can use break method for exiting in a function in js . for eg :

const getName = () => {
  getName: {
    console.log("I get logged");

    break getName;

    //exits the function

    console.log("I don't get logged");
  }
};
adiga
  • 34,372
  • 9
  • 61
  • 83