0

Here is a simplified version of what I have:

function test(thisBool){
 var response;
 if(thisBool){
 response = makeAPIrequest().then(function(){ 
 }, function(err){ 
 console.log(err)
 })
 } else {
 response = "doesntmatter"
 } 
}

So if the parameter is true, make the api request. If it's false, return the 'doesntmatter' string.

When I call this, I wish to use the response in either instance.

var testResponseTrue = test(true);
var testResponseFalse = test(false);

Is there a way to do this? Or will I have to do some callback functions somehow? I'm not sure of the best way to do this, any advice would help :)

Amir
1,3563 gold badges15 silver badges30 bronze badges
asked Jun 8, 2017 at 10:45
5
  • why not make an autoresolving promise in case it is false? Commented Jun 8, 2017 at 10:46
  • Is there a way to do this? - no, not like that Commented Jun 8, 2017 at 11:06
  • @JaromandaX he could if he used the async / await pattern ;) Commented Jun 8, 2017 at 11:25
  • ahh, @Icepickle - that would require an await inside an async function - so, not at all like the code he asked about ;) Commented Jun 8, 2017 at 11:28
  • @JaromandaX that's true, but it would be an extrapolation of what his desires are (as a former God you should know that ;) ) Commented Jun 8, 2017 at 11:36

3 Answers 3

2

Return a promise for each condition. You already have a promise being returned from your api request method so you can use Promise.resolve() for the default

function makeApiRequest(){ 
 // fake request ... use your normal method that already returns a prmise
 return new Promise(resolve=>{
 setTimeout(function(){
 resolve('Value from request')
 },500)
 });
 
}
function test(thisBool){
 return thisBool ? makeApiRequest() : Promise.resolve("Default value")
}
test(false).then(r => {console.log('False value:', r)});
test(true).then(r => {console.log('True value:', r)})

answered Jun 8, 2017 at 11:20
Sign up to request clarification or add additional context in comments.

Comments

1

You should use callback or promises to handle such kind of scenarios. Below is code snippet for the same using callback:

function test(thisBool, callback) {
 var response;
 if(thisBool) {
 makeAPIrequest()
 .then(function(response) {
 callback(response); 
 }, function(err) { 
 callback(err);
 })
 } else {
 response = "doesntmatter";
 callback(response);
 } 
}
test(true, function(testResponseTrue) {
 console.log(testResponseTrue);
});
test(false, function(testResponseFalse) {
 console.log(testResponseFalse);
});
answered Jun 8, 2017 at 11:01

Comments

1

It makes it a bit complicated that your consumers would have to check if they now got a promise back or not. I would rather choose to internally go for an autoresolving promise in case the boolean is false, so that you can handle the response in the same way as you would with a boolean that is true

// mock
function makeAPIrequest() {
 return new Promise( (resolve) => setTimeout( () => resolve('done'), 100 ) );
}
function test(thisBool){
 return new Promise( (resolve, reject) => {
 if (thisBool) {
 makeAPIrequest().then( result => resolve(result) ).catch( err => reject(err) );
 return;
 }
 setTimeout( () => resolve('does not matter'), 0);
 });
}
test(true).then( response => console.log('response from true = ' + response ) );
test(false).then( response => console.log('response from false = ' + response ) );

If you really want to catch the values in variables, I guess you could also use the async/await pattern, like in the following snippet

(async () => { // mock
 function makeAPIrequest() {
 return new Promise( (resolve) => setTimeout( () => resolve('done'), 100 ) );
 }
 let test = function async(thisBool){
 return new Promise( (resolve, reject) => {
 if (thisBool) {
 makeAPIrequest().then( result => resolve(result) ).catch( err => reject(err) );
 return;
 }
 setTimeout( () => resolve('does not matter'), 0);
 });
 }
 var testResponse1 = await test(true);
 var testResponse2 = await test(false);
 console.log( testResponse1 );
 console.log( testResponse2 );
})();

answered Jun 8, 2017 at 10:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.