これは、AWS Lambdaで公開するために作成した、自分のアプリケーションのnodeJSコードナゲットです。 CallProcessを機能は、基本的に私が渡しています都市についてのいくつかの処理された情報を返します - ハード「ニューヨーク」のためにここにコード化をコールバック関数内のnodeJSの変数のスコープ
function speech2(intent, session, callback) {
let country;
const repromptText = null;
const sessionAttributes = {};
let shouldEndSession = false;
let speechOutput = 'old text';
callProcess('New York', function (error, data) {
if (!error) {
speechOutput = data;
console.log(speechOutput);
}
else {
console.log(error.message);
}
});
// 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));
}
にconsole.log(speechOutput)正しく市に関する処理された情報を表示する - つまりCallProcessをが働いています。しかし、speechOutputを持つこの関数の最後のコールバックは、まだ '古いテキスト'を参照しています。つまり、関数内にある処理済み情報を使用して変数を上書きすることができません。コールバック内でこれをどうやって行うのですか?
ここのお手伝いをよろしくお願いいたします。前もって感謝します。
' CallProcessを() '、へので、あなたのコールバック(私は仮定)非同期です'callProcess'はあなたが' callback() 'を最後まで呼び出すまで起動しません。その値を取得するには、 'callProcess()'コールバックの中から 'callback()'を呼び出す必要があります。 –