How do I call 2 or more asynchronous methods in Actions on Google intent fulfilment V2












0















I had managed to call these 2 methods (geocoder.reverse, timezone.data) one after the other successfully in V1 DialogflowApp, but I am now upgrading to V2, and can't seem to get both asynchronous method calls right. Please help. I use these packages respectively





  • node-geocoder: ^3.22.0


  • node-google-timezone: ^0.1.1



         const options = { provider: 'google', httpAdapter: 'https', apiKey: googleApi, formatter: 'json'};
    const geocoder = NodeGeocoder(options);
    const timezone = require('node-google-timezone');

    app.intent('saving_prompt', (conv) => {
    if (conv.data.area) {
    const deviceCoordinatesStart = conv.device.location.coordinates;
    const latitudeValueStart = deviceCoordinatesStart.latitude;
    const longitudeValueStart = deviceCoordinatesStart.longitude;
    let start = moment(); //Start time in UTC, you could also use dateTime()
    let startTz = momentTz(); //used because of getting the Timezone
    const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

    //GEOLOCATION REVERSAL (METHOD 1)
    geocoder.reverse({lat:latitudeValueStart, lon:longitudeValueStart}, (err, res) => {
    if (err) {
    console.log(err);
    }
    let startLocation = res[0].administrativeLevels.level1long;
    conv.data.startLocation = startLocation;

    //GETTING LOCAL TIMEZONE FROM LAT/LONG AND TIMESTAMP (METHOD 2)
    timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
    if (err) {
    console.log(err);
    }
    var zoneHolder = tz.raw_response.timeZoneId;
    const localTime = startTz.tz(zoneHolder).format('LLL');
    conv.data.localTime = localTime;
    app.ask('Your race starts by ' + localTime + 'at' + startLocation);
    conv.ask(new Suggestions('End Race'));
    });
    });
    } else { //Returned if permission not granted
    conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
    conv.ask(new Suggestions(['Locate me', 'Exit']));

    }
    });



//FUNCTION CREATED TO GET TIMEZONE WITHOUT 'node-google-timezone' PACKAGE



function callTimezoneApi(lat, lon) {
return new Promise((resolve, reject) => {
let path = '/maps/api/timezone/json?location=' + lat +
',' + lon + '&timestamp=' + ts + '&key=' + googleApi;
console.log('API Request: ' + host + path);
// Make the HTTP request to get the weather
http.get({
host: host,
path: path
}, (res) => {
let body = ''; // var to store the response chunks
res.on('data', (d) => {
body += d;
}); // store each response chunk
res.on('end', () => {
let response = JSON.parse(body);
let zoneHolder = response.timeZoneId;
let startTz = momentTz(); //used in fulfilling the Timezone
const localTime = startTz.tz(zoneHolder).format('LLL');
console.log(localTime);
return resolve(localTime);
});
res.on('error', (error) => {reject(error);});
});
});
}









share|improve this question




















  • 1





    Can you update your question to include the packages you're using for geocode and timezone and their declaration?

    – Prisoner
    Dec 11 '18 at 15:30











  • Thanks. I just made updates as requested.

    – Ade
    Dec 11 '18 at 17:53











  • The start variable is never used.

    – Dennis Alund
    Dec 13 '18 at 1:44











  • I must have committed here, It is actually used - by storing in a 'conv.data.start' for future recall.

    – Ade
    Dec 13 '18 at 11:14
















0















I had managed to call these 2 methods (geocoder.reverse, timezone.data) one after the other successfully in V1 DialogflowApp, but I am now upgrading to V2, and can't seem to get both asynchronous method calls right. Please help. I use these packages respectively





  • node-geocoder: ^3.22.0


  • node-google-timezone: ^0.1.1



         const options = { provider: 'google', httpAdapter: 'https', apiKey: googleApi, formatter: 'json'};
    const geocoder = NodeGeocoder(options);
    const timezone = require('node-google-timezone');

    app.intent('saving_prompt', (conv) => {
    if (conv.data.area) {
    const deviceCoordinatesStart = conv.device.location.coordinates;
    const latitudeValueStart = deviceCoordinatesStart.latitude;
    const longitudeValueStart = deviceCoordinatesStart.longitude;
    let start = moment(); //Start time in UTC, you could also use dateTime()
    let startTz = momentTz(); //used because of getting the Timezone
    const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

    //GEOLOCATION REVERSAL (METHOD 1)
    geocoder.reverse({lat:latitudeValueStart, lon:longitudeValueStart}, (err, res) => {
    if (err) {
    console.log(err);
    }
    let startLocation = res[0].administrativeLevels.level1long;
    conv.data.startLocation = startLocation;

    //GETTING LOCAL TIMEZONE FROM LAT/LONG AND TIMESTAMP (METHOD 2)
    timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
    if (err) {
    console.log(err);
    }
    var zoneHolder = tz.raw_response.timeZoneId;
    const localTime = startTz.tz(zoneHolder).format('LLL');
    conv.data.localTime = localTime;
    app.ask('Your race starts by ' + localTime + 'at' + startLocation);
    conv.ask(new Suggestions('End Race'));
    });
    });
    } else { //Returned if permission not granted
    conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
    conv.ask(new Suggestions(['Locate me', 'Exit']));

    }
    });



//FUNCTION CREATED TO GET TIMEZONE WITHOUT 'node-google-timezone' PACKAGE



function callTimezoneApi(lat, lon) {
return new Promise((resolve, reject) => {
let path = '/maps/api/timezone/json?location=' + lat +
',' + lon + '&timestamp=' + ts + '&key=' + googleApi;
console.log('API Request: ' + host + path);
// Make the HTTP request to get the weather
http.get({
host: host,
path: path
}, (res) => {
let body = ''; // var to store the response chunks
res.on('data', (d) => {
body += d;
}); // store each response chunk
res.on('end', () => {
let response = JSON.parse(body);
let zoneHolder = response.timeZoneId;
let startTz = momentTz(); //used in fulfilling the Timezone
const localTime = startTz.tz(zoneHolder).format('LLL');
console.log(localTime);
return resolve(localTime);
});
res.on('error', (error) => {reject(error);});
});
});
}









share|improve this question




















  • 1





    Can you update your question to include the packages you're using for geocode and timezone and their declaration?

    – Prisoner
    Dec 11 '18 at 15:30











  • Thanks. I just made updates as requested.

    – Ade
    Dec 11 '18 at 17:53











  • The start variable is never used.

    – Dennis Alund
    Dec 13 '18 at 1:44











  • I must have committed here, It is actually used - by storing in a 'conv.data.start' for future recall.

    – Ade
    Dec 13 '18 at 11:14














0












0








0








I had managed to call these 2 methods (geocoder.reverse, timezone.data) one after the other successfully in V1 DialogflowApp, but I am now upgrading to V2, and can't seem to get both asynchronous method calls right. Please help. I use these packages respectively





  • node-geocoder: ^3.22.0


  • node-google-timezone: ^0.1.1



         const options = { provider: 'google', httpAdapter: 'https', apiKey: googleApi, formatter: 'json'};
    const geocoder = NodeGeocoder(options);
    const timezone = require('node-google-timezone');

    app.intent('saving_prompt', (conv) => {
    if (conv.data.area) {
    const deviceCoordinatesStart = conv.device.location.coordinates;
    const latitudeValueStart = deviceCoordinatesStart.latitude;
    const longitudeValueStart = deviceCoordinatesStart.longitude;
    let start = moment(); //Start time in UTC, you could also use dateTime()
    let startTz = momentTz(); //used because of getting the Timezone
    const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

    //GEOLOCATION REVERSAL (METHOD 1)
    geocoder.reverse({lat:latitudeValueStart, lon:longitudeValueStart}, (err, res) => {
    if (err) {
    console.log(err);
    }
    let startLocation = res[0].administrativeLevels.level1long;
    conv.data.startLocation = startLocation;

    //GETTING LOCAL TIMEZONE FROM LAT/LONG AND TIMESTAMP (METHOD 2)
    timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
    if (err) {
    console.log(err);
    }
    var zoneHolder = tz.raw_response.timeZoneId;
    const localTime = startTz.tz(zoneHolder).format('LLL');
    conv.data.localTime = localTime;
    app.ask('Your race starts by ' + localTime + 'at' + startLocation);
    conv.ask(new Suggestions('End Race'));
    });
    });
    } else { //Returned if permission not granted
    conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
    conv.ask(new Suggestions(['Locate me', 'Exit']));

    }
    });



//FUNCTION CREATED TO GET TIMEZONE WITHOUT 'node-google-timezone' PACKAGE



function callTimezoneApi(lat, lon) {
return new Promise((resolve, reject) => {
let path = '/maps/api/timezone/json?location=' + lat +
',' + lon + '&timestamp=' + ts + '&key=' + googleApi;
console.log('API Request: ' + host + path);
// Make the HTTP request to get the weather
http.get({
host: host,
path: path
}, (res) => {
let body = ''; // var to store the response chunks
res.on('data', (d) => {
body += d;
}); // store each response chunk
res.on('end', () => {
let response = JSON.parse(body);
let zoneHolder = response.timeZoneId;
let startTz = momentTz(); //used in fulfilling the Timezone
const localTime = startTz.tz(zoneHolder).format('LLL');
console.log(localTime);
return resolve(localTime);
});
res.on('error', (error) => {reject(error);});
});
});
}









share|improve this question
















I had managed to call these 2 methods (geocoder.reverse, timezone.data) one after the other successfully in V1 DialogflowApp, but I am now upgrading to V2, and can't seem to get both asynchronous method calls right. Please help. I use these packages respectively





  • node-geocoder: ^3.22.0


  • node-google-timezone: ^0.1.1



         const options = { provider: 'google', httpAdapter: 'https', apiKey: googleApi, formatter: 'json'};
    const geocoder = NodeGeocoder(options);
    const timezone = require('node-google-timezone');

    app.intent('saving_prompt', (conv) => {
    if (conv.data.area) {
    const deviceCoordinatesStart = conv.device.location.coordinates;
    const latitudeValueStart = deviceCoordinatesStart.latitude;
    const longitudeValueStart = deviceCoordinatesStart.longitude;
    let start = moment(); //Start time in UTC, you could also use dateTime()
    let startTz = momentTz(); //used because of getting the Timezone
    const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

    //GEOLOCATION REVERSAL (METHOD 1)
    geocoder.reverse({lat:latitudeValueStart, lon:longitudeValueStart}, (err, res) => {
    if (err) {
    console.log(err);
    }
    let startLocation = res[0].administrativeLevels.level1long;
    conv.data.startLocation = startLocation;

    //GETTING LOCAL TIMEZONE FROM LAT/LONG AND TIMESTAMP (METHOD 2)
    timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
    if (err) {
    console.log(err);
    }
    var zoneHolder = tz.raw_response.timeZoneId;
    const localTime = startTz.tz(zoneHolder).format('LLL');
    conv.data.localTime = localTime;
    app.ask('Your race starts by ' + localTime + 'at' + startLocation);
    conv.ask(new Suggestions('End Race'));
    });
    });
    } else { //Returned if permission not granted
    conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
    conv.ask(new Suggestions(['Locate me', 'Exit']));

    }
    });



//FUNCTION CREATED TO GET TIMEZONE WITHOUT 'node-google-timezone' PACKAGE



function callTimezoneApi(lat, lon) {
return new Promise((resolve, reject) => {
let path = '/maps/api/timezone/json?location=' + lat +
',' + lon + '&timestamp=' + ts + '&key=' + googleApi;
console.log('API Request: ' + host + path);
// Make the HTTP request to get the weather
http.get({
host: host,
path: path
}, (res) => {
let body = ''; // var to store the response chunks
res.on('data', (d) => {
body += d;
}); // store each response chunk
res.on('end', () => {
let response = JSON.parse(body);
let zoneHolder = response.timeZoneId;
let startTz = momentTz(); //used in fulfilling the Timezone
const localTime = startTz.tz(zoneHolder).format('LLL');
console.log(localTime);
return resolve(localTime);
});
res.on('error', (error) => {reject(error);});
});
});
}






javascript node.js actions-on-google dialogflow-fulfillment






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 22:46







Ade

















asked Dec 11 '18 at 15:24









AdeAde

153110




153110








  • 1





    Can you update your question to include the packages you're using for geocode and timezone and their declaration?

    – Prisoner
    Dec 11 '18 at 15:30











  • Thanks. I just made updates as requested.

    – Ade
    Dec 11 '18 at 17:53











  • The start variable is never used.

    – Dennis Alund
    Dec 13 '18 at 1:44











  • I must have committed here, It is actually used - by storing in a 'conv.data.start' for future recall.

    – Ade
    Dec 13 '18 at 11:14














  • 1





    Can you update your question to include the packages you're using for geocode and timezone and their declaration?

    – Prisoner
    Dec 11 '18 at 15:30











  • Thanks. I just made updates as requested.

    – Ade
    Dec 11 '18 at 17:53











  • The start variable is never used.

    – Dennis Alund
    Dec 13 '18 at 1:44











  • I must have committed here, It is actually used - by storing in a 'conv.data.start' for future recall.

    – Ade
    Dec 13 '18 at 11:14








1




1





Can you update your question to include the packages you're using for geocode and timezone and their declaration?

– Prisoner
Dec 11 '18 at 15:30





Can you update your question to include the packages you're using for geocode and timezone and their declaration?

– Prisoner
Dec 11 '18 at 15:30













Thanks. I just made updates as requested.

– Ade
Dec 11 '18 at 17:53





Thanks. I just made updates as requested.

– Ade
Dec 11 '18 at 17:53













The start variable is never used.

– Dennis Alund
Dec 13 '18 at 1:44





The start variable is never used.

– Dennis Alund
Dec 13 '18 at 1:44













I must have committed here, It is actually used - by storing in a 'conv.data.start' for future recall.

– Ade
Dec 13 '18 at 11:14





I must have committed here, It is actually used - by storing in a 'conv.data.start' for future recall.

– Ade
Dec 13 '18 at 11:14












1 Answer
1






active

oldest

votes


















1














If you use Node 8 you can use async/await to untangle the nested callback structure. It will ensure that your asynchronous calls are completed before proceeding with the next call, that has dependency on another async value.



app.intent('saving_prompt', async (conv) => {
if (conv.data.area) {
const deviceCoordinatesStart = conv.device.location.coordinates;
const latitudeValueStart = deviceCoordinatesStart.latitude;
const longitudeValueStart = deviceCoordinatesStart.longitude;
let start = moment(); //Start time in UTC, you could also use dateTime()
let startTz = momentTz(); //used because of getting the Timezone
const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

const startLocation = await new Promise((resolve, reject) => {
geocoder.reverse({ lat: latitudeValueStart, lon: longitudeValueStart }, (err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(res[0].administrativeLevels.level1long);
}
});
});

const localTime = await new Promise((resolve, reject) => {
timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
if (err) {
console.log(err);
reject(err);
} else {
var zoneHolder = tz.raw_response.timeZoneId;
resolve(startTz.tz(zoneHolder).format('LLL'));
}
});
});

app.ask('Your race starts by ' + localTime + 'at' + startLocation);
conv.ask(new Suggestions('End Race'));
} else { //Returned if permission not granted
conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
conv.ask(new Suggestions(['Locate me', 'Exit']));

}
});





share|improve this answer
























  • Thank you very much for the response, unfortunately Firebase functions - which is the runtime I use, is predicated on node 6, which does not support await/async. Else it would've been an easy wrap.

    – Ade
    Dec 13 '18 at 11:11













  • You can migrate it to node 8: cloud.google.com/functions/docs/concepts/nodejs-8-runtime

    – Dennis Alund
    Dec 13 '18 at 15:37











  • But I believe that migration is only applicable to cloud functions, and not firebase functions which Dialogflow V2 uses. When I had used similar code in Dialogflow V1 - then running on cloud functions, it had worked hitchlessly.

    – Ade
    Dec 14 '18 at 9:22











  • Cloud functions for firebase are the very same cloud functions.

    – Dennis Alund
    Dec 14 '18 at 9:28








  • 1





    firebase.googleblog.com/2018/08/…

    – Dennis Alund
    Dec 18 '18 at 7:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53727262%2fhow-do-i-call-2-or-more-asynchronous-methods-in-actions-on-google-intent-fulfilm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














If you use Node 8 you can use async/await to untangle the nested callback structure. It will ensure that your asynchronous calls are completed before proceeding with the next call, that has dependency on another async value.



app.intent('saving_prompt', async (conv) => {
if (conv.data.area) {
const deviceCoordinatesStart = conv.device.location.coordinates;
const latitudeValueStart = deviceCoordinatesStart.latitude;
const longitudeValueStart = deviceCoordinatesStart.longitude;
let start = moment(); //Start time in UTC, you could also use dateTime()
let startTz = momentTz(); //used because of getting the Timezone
const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

const startLocation = await new Promise((resolve, reject) => {
geocoder.reverse({ lat: latitudeValueStart, lon: longitudeValueStart }, (err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(res[0].administrativeLevels.level1long);
}
});
});

const localTime = await new Promise((resolve, reject) => {
timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
if (err) {
console.log(err);
reject(err);
} else {
var zoneHolder = tz.raw_response.timeZoneId;
resolve(startTz.tz(zoneHolder).format('LLL'));
}
});
});

app.ask('Your race starts by ' + localTime + 'at' + startLocation);
conv.ask(new Suggestions('End Race'));
} else { //Returned if permission not granted
conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
conv.ask(new Suggestions(['Locate me', 'Exit']));

}
});





share|improve this answer
























  • Thank you very much for the response, unfortunately Firebase functions - which is the runtime I use, is predicated on node 6, which does not support await/async. Else it would've been an easy wrap.

    – Ade
    Dec 13 '18 at 11:11













  • You can migrate it to node 8: cloud.google.com/functions/docs/concepts/nodejs-8-runtime

    – Dennis Alund
    Dec 13 '18 at 15:37











  • But I believe that migration is only applicable to cloud functions, and not firebase functions which Dialogflow V2 uses. When I had used similar code in Dialogflow V1 - then running on cloud functions, it had worked hitchlessly.

    – Ade
    Dec 14 '18 at 9:22











  • Cloud functions for firebase are the very same cloud functions.

    – Dennis Alund
    Dec 14 '18 at 9:28








  • 1





    firebase.googleblog.com/2018/08/…

    – Dennis Alund
    Dec 18 '18 at 7:18
















1














If you use Node 8 you can use async/await to untangle the nested callback structure. It will ensure that your asynchronous calls are completed before proceeding with the next call, that has dependency on another async value.



app.intent('saving_prompt', async (conv) => {
if (conv.data.area) {
const deviceCoordinatesStart = conv.device.location.coordinates;
const latitudeValueStart = deviceCoordinatesStart.latitude;
const longitudeValueStart = deviceCoordinatesStart.longitude;
let start = moment(); //Start time in UTC, you could also use dateTime()
let startTz = momentTz(); //used because of getting the Timezone
const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

const startLocation = await new Promise((resolve, reject) => {
geocoder.reverse({ lat: latitudeValueStart, lon: longitudeValueStart }, (err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(res[0].administrativeLevels.level1long);
}
});
});

const localTime = await new Promise((resolve, reject) => {
timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
if (err) {
console.log(err);
reject(err);
} else {
var zoneHolder = tz.raw_response.timeZoneId;
resolve(startTz.tz(zoneHolder).format('LLL'));
}
});
});

app.ask('Your race starts by ' + localTime + 'at' + startLocation);
conv.ask(new Suggestions('End Race'));
} else { //Returned if permission not granted
conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
conv.ask(new Suggestions(['Locate me', 'Exit']));

}
});





share|improve this answer
























  • Thank you very much for the response, unfortunately Firebase functions - which is the runtime I use, is predicated on node 6, which does not support await/async. Else it would've been an easy wrap.

    – Ade
    Dec 13 '18 at 11:11













  • You can migrate it to node 8: cloud.google.com/functions/docs/concepts/nodejs-8-runtime

    – Dennis Alund
    Dec 13 '18 at 15:37











  • But I believe that migration is only applicable to cloud functions, and not firebase functions which Dialogflow V2 uses. When I had used similar code in Dialogflow V1 - then running on cloud functions, it had worked hitchlessly.

    – Ade
    Dec 14 '18 at 9:22











  • Cloud functions for firebase are the very same cloud functions.

    – Dennis Alund
    Dec 14 '18 at 9:28








  • 1





    firebase.googleblog.com/2018/08/…

    – Dennis Alund
    Dec 18 '18 at 7:18














1












1








1







If you use Node 8 you can use async/await to untangle the nested callback structure. It will ensure that your asynchronous calls are completed before proceeding with the next call, that has dependency on another async value.



app.intent('saving_prompt', async (conv) => {
if (conv.data.area) {
const deviceCoordinatesStart = conv.device.location.coordinates;
const latitudeValueStart = deviceCoordinatesStart.latitude;
const longitudeValueStart = deviceCoordinatesStart.longitude;
let start = moment(); //Start time in UTC, you could also use dateTime()
let startTz = momentTz(); //used because of getting the Timezone
const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

const startLocation = await new Promise((resolve, reject) => {
geocoder.reverse({ lat: latitudeValueStart, lon: longitudeValueStart }, (err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(res[0].administrativeLevels.level1long);
}
});
});

const localTime = await new Promise((resolve, reject) => {
timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
if (err) {
console.log(err);
reject(err);
} else {
var zoneHolder = tz.raw_response.timeZoneId;
resolve(startTz.tz(zoneHolder).format('LLL'));
}
});
});

app.ask('Your race starts by ' + localTime + 'at' + startLocation);
conv.ask(new Suggestions('End Race'));
} else { //Returned if permission not granted
conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
conv.ask(new Suggestions(['Locate me', 'Exit']));

}
});





share|improve this answer













If you use Node 8 you can use async/await to untangle the nested callback structure. It will ensure that your asynchronous calls are completed before proceeding with the next call, that has dependency on another async value.



app.intent('saving_prompt', async (conv) => {
if (conv.data.area) {
const deviceCoordinatesStart = conv.device.location.coordinates;
const latitudeValueStart = deviceCoordinatesStart.latitude;
const longitudeValueStart = deviceCoordinatesStart.longitude;
let start = moment(); //Start time in UTC, you could also use dateTime()
let startTz = momentTz(); //used because of getting the Timezone
const timestamp = 1402629305; // Just a dud placeholder to fulfil timezone function

const startLocation = await new Promise((resolve, reject) => {
geocoder.reverse({ lat: latitudeValueStart, lon: longitudeValueStart }, (err, res) => {
if (err) {
console.log(err);
reject(err);
} else {
resolve(res[0].administrativeLevels.level1long);
}
});
});

const localTime = await new Promise((resolve, reject) => {
timezone.data(latitudeValueStart, longitudeValueStart, timestamp, (err, tz) => {
if (err) {
console.log(err);
reject(err);
} else {
var zoneHolder = tz.raw_response.timeZoneId;
resolve(startTz.tz(zoneHolder).format('LLL'));
}
});
});

app.ask('Your race starts by ' + localTime + 'at' + startLocation);
conv.ask(new Suggestions('End Race'));
} else { //Returned if permission not granted
conv.ask('You do not have your location on. Say "Locate me", to retrieve your location');
conv.ask(new Suggestions(['Locate me', 'Exit']));

}
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 13 '18 at 1:46









Dennis AlundDennis Alund

609216




609216













  • Thank you very much for the response, unfortunately Firebase functions - which is the runtime I use, is predicated on node 6, which does not support await/async. Else it would've been an easy wrap.

    – Ade
    Dec 13 '18 at 11:11













  • You can migrate it to node 8: cloud.google.com/functions/docs/concepts/nodejs-8-runtime

    – Dennis Alund
    Dec 13 '18 at 15:37











  • But I believe that migration is only applicable to cloud functions, and not firebase functions which Dialogflow V2 uses. When I had used similar code in Dialogflow V1 - then running on cloud functions, it had worked hitchlessly.

    – Ade
    Dec 14 '18 at 9:22











  • Cloud functions for firebase are the very same cloud functions.

    – Dennis Alund
    Dec 14 '18 at 9:28








  • 1





    firebase.googleblog.com/2018/08/…

    – Dennis Alund
    Dec 18 '18 at 7:18



















  • Thank you very much for the response, unfortunately Firebase functions - which is the runtime I use, is predicated on node 6, which does not support await/async. Else it would've been an easy wrap.

    – Ade
    Dec 13 '18 at 11:11













  • You can migrate it to node 8: cloud.google.com/functions/docs/concepts/nodejs-8-runtime

    – Dennis Alund
    Dec 13 '18 at 15:37











  • But I believe that migration is only applicable to cloud functions, and not firebase functions which Dialogflow V2 uses. When I had used similar code in Dialogflow V1 - then running on cloud functions, it had worked hitchlessly.

    – Ade
    Dec 14 '18 at 9:22











  • Cloud functions for firebase are the very same cloud functions.

    – Dennis Alund
    Dec 14 '18 at 9:28








  • 1





    firebase.googleblog.com/2018/08/…

    – Dennis Alund
    Dec 18 '18 at 7:18

















Thank you very much for the response, unfortunately Firebase functions - which is the runtime I use, is predicated on node 6, which does not support await/async. Else it would've been an easy wrap.

– Ade
Dec 13 '18 at 11:11







Thank you very much for the response, unfortunately Firebase functions - which is the runtime I use, is predicated on node 6, which does not support await/async. Else it would've been an easy wrap.

– Ade
Dec 13 '18 at 11:11















You can migrate it to node 8: cloud.google.com/functions/docs/concepts/nodejs-8-runtime

– Dennis Alund
Dec 13 '18 at 15:37





You can migrate it to node 8: cloud.google.com/functions/docs/concepts/nodejs-8-runtime

– Dennis Alund
Dec 13 '18 at 15:37













But I believe that migration is only applicable to cloud functions, and not firebase functions which Dialogflow V2 uses. When I had used similar code in Dialogflow V1 - then running on cloud functions, it had worked hitchlessly.

– Ade
Dec 14 '18 at 9:22





But I believe that migration is only applicable to cloud functions, and not firebase functions which Dialogflow V2 uses. When I had used similar code in Dialogflow V1 - then running on cloud functions, it had worked hitchlessly.

– Ade
Dec 14 '18 at 9:22













Cloud functions for firebase are the very same cloud functions.

– Dennis Alund
Dec 14 '18 at 9:28







Cloud functions for firebase are the very same cloud functions.

– Dennis Alund
Dec 14 '18 at 9:28






1




1





firebase.googleblog.com/2018/08/…

– Dennis Alund
Dec 18 '18 at 7:18





firebase.googleblog.com/2018/08/…

– Dennis Alund
Dec 18 '18 at 7:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53727262%2fhow-do-i-call-2-or-more-asynchronous-methods-in-actions-on-google-intent-fulfilm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas