Reputation: 305
I have a function that returns a Promise
function superHero_with_name (name) {
return new Promise((resolve, reject) => {
var encoded_superhero_name = name.split(" ").join("%20");
var path_superhero = '/api.php/' + superhero_access_token + '/search/' + encoded_superhero_name ;
console.log('API Request: ' + host_superhero + path_superhero);
http.get({host: host_superhero, path: path_superhero}, (res) => {
let body = '';
res.on('data', (d) => { body += d;});
res.on('end', () => {
// console.log("BODY:", body);
let output = JSON.parse(body);
resolve(output);
});
res.on('error', (error) => {
console.log(`Error while calling the API ${error}` );
});
});
});
}
I am using Actions SDK and the error thrown to me after calling this function is
No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?
And the function in which I am calling this function is
superhero.intent('superherocard', (conv, {superhero_entity}) => {
superHero_with_name(superhero_entity).then((output)=>{
if(output.response === 'success'){
var super_name = output.results[0].name ;
// Assigned values to all the variables used below this comment
conv.ask("Here's your Superhero");
conv.ask(new BasicCard({
text: `Your SuperHero is the mighty **${super_name}**. \n**INTELLIGENCE** *${intelligence}* \n**STRENGTH** *${strength}* \n**SPEED** *${speed}* \n**DURABILITY** *${durability}* \n**POWER** *${power}* \n**COMBAT** *${combat}*`,
title: super_name,
subtitle: super_publisher,
image: new Image({
url: image_superhero,
alt: super_name,
}),
}));
conv.ask(text_1);
conv.ask(new Suggestions(itemSuggestions));
} else{
conv.ask('Sorry, I cannot find the superhero you are finding! But you can become one by helping and motivating people.');
}
return console.log("superHeroCard executed");
}).catch(()=> {
conv.ask('Sorry, I cannot find the superhero you are finding! But you can become one by helping and motivating people.');
});
});
I am unable to find the error as I am returning the Promise but the intent handler is unable to read it.
Upvotes: 2
Views: 720
Reputation: 50741
The issue is that the Intent Handler is reading it, but it isn't returning it.
If you are doing async calls, your handler must return a Promise that will resolve when the async portion is done. If you are not doing async calls, then you don't need to return a promise - since the handler dispatcher doesn't need to wait for anything before it sends back a reply. If you are doing an async call, a Promise indicates to the dispatcher that it needs to wait for the Promise to be fully resolved before it can return the response that you've set.
In your case, you can probably adjust the first two lines of the handler to return the Promise that is returned by the function call and then()
chain. Possibly something like:
superhero.intent('superherocard', (conv, {superhero_entity}) => {
return superHero_with_name(superhero_entity).then((output)=>{
// ...
Upvotes: 2