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 :)
3 Answers 3
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)})
Comments
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);
});
Comments
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 );
})();
Is there a way to do this?- no, not like thatawaitinside anasyncfunction - so, not at all like the code he asked about ;)