[英]aws lambda nodejs accessing value of asynchronous function in another async function
我有一個函數,我在其中獲取firebase數據值,即聯系:
function firebaseData (callback) {
if(firebase.apps.length == 0) {
firebase.initializeApp({
serviceAccount: "./Sample1-bc6d1ce099d8.json",
databaseURL:"https://sample1-74bc2.firebaseio.com/"
});
}
var ref = firebase.database().ref();
var usersRef = ref.child('users');
usersRef.once('value').then(function (snap) {
snap.forEach(function(childSnapshot) {
var childKey = childSnapshot.val();
var contact = childKey.Contact;
callback(contact)
});
});
}
我想要的是在聯系人的其他功能中使用此聯系值數據:
function getContactFromSession(intent, session, callback) {
const repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
let speechOutput = "amy's contact is"+CONTACT;
// Setting repromptText to null signifies that we do not want to reprompt the user.
// If the user does not respond or says something that is not understood, the session
// will end.
callback(
sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession)
);
}
那怎么辦,有人可以建議我嗎?
也許是這樣的嗎? 調用的回調getContactFromSession()
的回調內部firebaseData()
function getContactFromSession(intent, session, callback) {
const repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
firebaseData(function (contact) {
let speechOutput = "amy's contact is " + contact;
callback(
sessionAttributes,
buildSpeechletResponse(intent.name, speechOutput, repromptText, shouldEndSession)
);
})
}
使用Promises可能有助於使其更具可讀性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.