How to wrap a conditional statement in a Promise [duplicate]












0
















This question already has an answer here:




  • if-else flow in promise (bluebird)

    3 answers




I have a conditional if statement who's logic needs to be wrapped in a promise as the logic after if can only be executed after it.



// WRAP 'if' statement below IN A PROMISE
if (a) { // could be true or false. If false, resolve the promise
// logic here
}

.then(
// execute logic after if here
)


I'm new to nodejs and am trying to wrap my head around this. How can I solve this ?










share|improve this question













marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 15:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Please, can you restructure your question for easy understanding?

    – Cocest
    Jan 1 at 13:18






  • 1





    If you don't have any asynchronous processing, you don't need promises. Any statements you want to execute after other statements (like an if block) should just follow after those statements in the same block. If you do have some asynchronous API call, then please show this in your code.

    – trincot
    Jan 1 at 13:25
















0
















This question already has an answer here:




  • if-else flow in promise (bluebird)

    3 answers




I have a conditional if statement who's logic needs to be wrapped in a promise as the logic after if can only be executed after it.



// WRAP 'if' statement below IN A PROMISE
if (a) { // could be true or false. If false, resolve the promise
// logic here
}

.then(
// execute logic after if here
)


I'm new to nodejs and am trying to wrap my head around this. How can I solve this ?










share|improve this question













marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 15:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Please, can you restructure your question for easy understanding?

    – Cocest
    Jan 1 at 13:18






  • 1





    If you don't have any asynchronous processing, you don't need promises. Any statements you want to execute after other statements (like an if block) should just follow after those statements in the same block. If you do have some asynchronous API call, then please show this in your code.

    – trincot
    Jan 1 at 13:25














0












0








0









This question already has an answer here:




  • if-else flow in promise (bluebird)

    3 answers




I have a conditional if statement who's logic needs to be wrapped in a promise as the logic after if can only be executed after it.



// WRAP 'if' statement below IN A PROMISE
if (a) { // could be true or false. If false, resolve the promise
// logic here
}

.then(
// execute logic after if here
)


I'm new to nodejs and am trying to wrap my head around this. How can I solve this ?










share|improve this question















This question already has an answer here:




  • if-else flow in promise (bluebird)

    3 answers




I have a conditional if statement who's logic needs to be wrapped in a promise as the logic after if can only be executed after it.



// WRAP 'if' statement below IN A PROMISE
if (a) { // could be true or false. If false, resolve the promise
// logic here
}

.then(
// execute logic after if here
)


I'm new to nodejs and am trying to wrap my head around this. How can I solve this ?





This question already has an answer here:




  • if-else flow in promise (bluebird)

    3 answers








javascript node.js promise






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 12:21









tsaebehttsaebeht

1762418




1762418




marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 15:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 15:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Please, can you restructure your question for easy understanding?

    – Cocest
    Jan 1 at 13:18






  • 1





    If you don't have any asynchronous processing, you don't need promises. Any statements you want to execute after other statements (like an if block) should just follow after those statements in the same block. If you do have some asynchronous API call, then please show this in your code.

    – trincot
    Jan 1 at 13:25



















  • Please, can you restructure your question for easy understanding?

    – Cocest
    Jan 1 at 13:18






  • 1





    If you don't have any asynchronous processing, you don't need promises. Any statements you want to execute after other statements (like an if block) should just follow after those statements in the same block. If you do have some asynchronous API call, then please show this in your code.

    – trincot
    Jan 1 at 13:25

















Please, can you restructure your question for easy understanding?

– Cocest
Jan 1 at 13:18





Please, can you restructure your question for easy understanding?

– Cocest
Jan 1 at 13:18




1




1





If you don't have any asynchronous processing, you don't need promises. Any statements you want to execute after other statements (like an if block) should just follow after those statements in the same block. If you do have some asynchronous API call, then please show this in your code.

– trincot
Jan 1 at 13:25





If you don't have any asynchronous processing, you don't need promises. Any statements you want to execute after other statements (like an if block) should just follow after those statements in the same block. If you do have some asynchronous API call, then please show this in your code.

– trincot
Jan 1 at 13:25












2 Answers
2






active

oldest

votes


















1














Just wrap it into a new Promise:



new Promise((resolve, reject) => {
if(a){
reject("error");
} else {
resolve(yourData);
}
})
.then(data => {
// Do stuff
})
.catch(err => {
// You should catch here an error rejected above
})





share|improve this answer































    0














    The question is unclear a bit. Here is answer according to what I understand. You can not run code inside promise once it is resolved or rejected.



    new Promise((res, rej) => {

    if (false) {


    }
    res();
    console.log('More code') // this will not run

    }).then(() => {

    console.log('This will run')

    })





    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Just wrap it into a new Promise:



      new Promise((resolve, reject) => {
      if(a){
      reject("error");
      } else {
      resolve(yourData);
      }
      })
      .then(data => {
      // Do stuff
      })
      .catch(err => {
      // You should catch here an error rejected above
      })





      share|improve this answer




























        1














        Just wrap it into a new Promise:



        new Promise((resolve, reject) => {
        if(a){
        reject("error");
        } else {
        resolve(yourData);
        }
        })
        .then(data => {
        // Do stuff
        })
        .catch(err => {
        // You should catch here an error rejected above
        })





        share|improve this answer


























          1












          1








          1







          Just wrap it into a new Promise:



          new Promise((resolve, reject) => {
          if(a){
          reject("error");
          } else {
          resolve(yourData);
          }
          })
          .then(data => {
          // Do stuff
          })
          .catch(err => {
          // You should catch here an error rejected above
          })





          share|improve this answer













          Just wrap it into a new Promise:



          new Promise((resolve, reject) => {
          if(a){
          reject("error");
          } else {
          resolve(yourData);
          }
          })
          .then(data => {
          // Do stuff
          })
          .catch(err => {
          // You should catch here an error rejected above
          })






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 13:16









          Slawomir WozniakSlawomir Wozniak

          344210




          344210

























              0














              The question is unclear a bit. Here is answer according to what I understand. You can not run code inside promise once it is resolved or rejected.



              new Promise((res, rej) => {

              if (false) {


              }
              res();
              console.log('More code') // this will not run

              }).then(() => {

              console.log('This will run')

              })





              share|improve this answer




























                0














                The question is unclear a bit. Here is answer according to what I understand. You can not run code inside promise once it is resolved or rejected.



                new Promise((res, rej) => {

                if (false) {


                }
                res();
                console.log('More code') // this will not run

                }).then(() => {

                console.log('This will run')

                })





                share|improve this answer


























                  0












                  0








                  0







                  The question is unclear a bit. Here is answer according to what I understand. You can not run code inside promise once it is resolved or rejected.



                  new Promise((res, rej) => {

                  if (false) {


                  }
                  res();
                  console.log('More code') // this will not run

                  }).then(() => {

                  console.log('This will run')

                  })





                  share|improve this answer













                  The question is unclear a bit. Here is answer according to what I understand. You can not run code inside promise once it is resolved or rejected.



                  new Promise((res, rej) => {

                  if (false) {


                  }
                  res();
                  console.log('More code') // this will not run

                  }).then(() => {

                  console.log('This will run')

                  })






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 13:21









                  Nafees AnwarNafees Anwar

                  306115




                  306115















                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas