How to wrap a conditional statement in a Promise [duplicate]
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 ?
javascript node.js promise
marked as duplicate by Bergi
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.
add a comment |
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 ?
javascript node.js promise
marked as duplicate by Bergi
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 anif
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
add a comment |
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 ?
javascript node.js promise
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
javascript node.js promise
asked Jan 1 at 12:21
tsaebehttsaebeht
1762418
1762418
marked as duplicate by Bergi
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
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 anif
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
add a comment |
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 anif
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
add a comment |
2 Answers
2
active
oldest
votes
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
})
add a comment |
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')
})
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
})
add a comment |
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
})
add a comment |
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
})
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
})
answered Jan 1 at 13:16
Slawomir WozniakSlawomir Wozniak
344210
344210
add a comment |
add a comment |
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')
})
add a comment |
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')
})
add a comment |
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')
})
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')
})
answered Jan 1 at 13:21
Nafees AnwarNafees Anwar
306115
306115
add a comment |
add a comment |
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