JavaScript How do I conditionally run a .then() function in a .then .catch chain?

My code resembles this:

router.post("/", (req, res, next) => {
foo()
.then((result) => {
    res.send(result)
})
.catch((error) => {
    cosnole.log(error)
})
//I only want the bar() function and everything below it to run if the first promise is rejected and the first .catch function ran
bar()
.then((data) => {
    res.send(data)
})
.catch((error) => {
    console.log(error)
})

})

I'd like to only run the bar() function and the .then .catch functions after it only if the first promise is rejected and the .catch function fires off.

I've tried this:

router.post("/", (req, res, next) => {
rejected = false
foo()
.then((result) => {
    res.send(result)
})
.catch((error) => {
    rejected = true
    console.log(error)
})
if(rejected == true)
    bar()
    .then((data) => {
        res.send(data)
    })
    .catch((error) => {
        console.log(error)
    })

})

but the bar() function never gets executed when the first foo() function's error is caught and the promise is rejected.

Answer:1

I/O calls happens asynchronously, so your if code gets running before res.send() return any response. There are several ways to handle this scenario, here is another way using asyn/await in order to write code which looks like synchronous:

router.post("/", async (req, res, next) => {
rejected = false
try {
  await foo()
    .then((result) => {
      res.send(result)
    })
} catch {
  rejected = true
  console.log(error)
}
if(rejected == true)
  bar()
  .then((data) => {
    res.send(data)
  })
  .catch((error) => {
    console.log(error)
  })
})
Answer:2

Given the asynchronous nature of your code, if (rejected==true) will always be false, because that code is executed before the first then or catch.

You can:

Move all the bar()[...] code inside the .catch()

foo()
.then((result) => {
    res.send(result)
})
.catch((error) => {
    console.log(error)
    bar()
    .then((data) => {
        res.send(data)
    })
    .catch((error) => {
        console.log(error)
    })
})

move all the conditional execution inside a .then() after the .catch()

rejected = false
foo()
.then((result) => {
    res.send(result)
})
.catch((error) => {
    console.log(error)
    rejected = true
})
.then(() => {
  if(rejected) {
    bar()
    .then((data) => {
        res.send(data)
    })
    .catch((error) => {
        console.log(error)
    })
  }
})
Answer:3

I'm looking for maven config to minify js/css files for a spring boot application. For a normal java web app the following config works <plugins> <plugin> ...

I'm looking for maven config to minify js/css files for a spring boot application. For a normal java web app the following config works <plugins> <plugin> ...

  1. spring boot minify javascript
  2. spring boot minify html
  3. spring boot minify css
  4. spring boot minify
  5. spring boot minify-maven-plugin
  6. spring boot minification
  7. spring boot thymeleaf minify
  8. spring boot json minify
  9. spring boot maven minify

In my php code, I have two dropdowns which is the other one depends on the first one. Here is the first dropdown: <select class="form-control style" id="sel" > <option style="color: ...

In my php code, I have two dropdowns which is the other one depends on the first one. Here is the first dropdown: <select class="form-control style" id="sel" > <option style="color: ...

  1. found using wooded kingdom art
  2. found using rox spray underpass
  3. found using bunker jonesy
  4. found using cap kingdom art
  5. found using argument-dependent lookup
  6. found using seaside kingdom art
  7. found using bot spray
  8. found using bunker basher
  9. found using mushroom kingdom art
  10. found using vega outfit
  11. found using bowser kingdom art
  12. found using moon kingdom art
  13. found using rock love spray
  14. found using sunbird spray
  15. found using words
  16. command not found when using sudo

I need to create an empty JQuery element to be the outer scope list of a loop, for example : const forms_data = ['Name', 'Email']; const form = $(); for (const input_name of forms_data){ form....

I need to create an empty JQuery element to be the outer scope list of a loop, for example : const forms_data = ['Name', 'Email']; const form = $(); for (const input_name of forms_data){ form....

  1. create empty jquery object
  2. create empty jquery array
  3. create empty jquery collection
  4. create empty jquery element
  5. create empty jquery
  6. create empty div jquery
  7. create empty datatable jquery
  8. create empty list jquery
  9. jquery create empty json object
  10. jquery create empty array and push
  11. jquery create empty dom element
  12. jquery create empty selector
  13. jquery create empty variable
  14. jquery create empty selection
  15. jquery create empty object array
  16. jquery create empty html element
  17. jquery create empty promise
  18. jquery create empty json
  19. jquery create empty guid
  20. jquery create empty attribute

I've been searching for the answer to this question but have so far been unable to piece together the answer. Please explain any answer you have in really simple terms as I'm fairly new to GAS and ...

I've been searching for the answer to this question but have so far been unable to piece together the answer. Please explain any answer you have in really simple terms as I'm fairly new to GAS and ...

  1. regex replace tag html
  2. regex replace tags
  3. regex replace html tags c#
  4. regex replace html tags javascript
  5. regex replace script tags
  6. regex replace between tags
  7. regex replace style tags
  8. php regex replace tags
  9. regex replace all tags
  10. regex replace html tags except
  11. regex replace anchor tags
  12. javascript regex replace tags
  13. python regex replace tags
  14. regex replace xml tags
  15. c# regex replace tags
  16. regex replace br tags
  17. java regex replace tags
  18. regex replace html tags java
  19. regex replace href tags
  20. regex replace img tags