Node JS 嵌套函数被跳过
Node JS nested function is being skipped
尝试从 Alexa Intent 读取 dynamoDB table,当我执行以下代码块时,getItem() 方法被跳过
function alexaIntent(intent, session, callback) {
var results = "";
var params = {
Key: {
"name": {
S: "aaa"
}
},
TableName: "bbb"
};
console.log('1111111111111111111111111111111');
/* Code from here till.....*/
ddb.getItem(params, (err, data) => {
console.log('inside');
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});
/* .....here is being skipped */
console.log('2222222');
callback(session.attributes,
buildSpeechletResponseWithoutCard("I am the King!!", "", "true"));
}
我是异步编程的新手,我是否缺少基本概念/理解?
我得到的输出是:
1111111111111111111111111111111
2222222
没有被跳过。只是non blocking
。传递给 getItem
的第二个参数 ((err, data) => {...
) 的全部意义在于让您知道一旦执行完毕。此外,您看不到 console.error("Unable to read item...
或 console.log("GetItem succeeded:" ...
的原因是因为您告诉 alexaIntent 在等待 getItem
.
之前已完成
只需将最后一个回调调用移至提供给 getItem
的回调内部:
ddb.getItem(params, (err, data) => {
console.log('inside');
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
callback(session.attributes, buildSpeechletResponseWithoutCard("Error!!", "", "true"));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
callback(session.attributes,
buildSpeechletResponseWithoutCard("I am the King!!", "", "true"));
}
});
尝试从 Alexa Intent 读取 dynamoDB table,当我执行以下代码块时,getItem() 方法被跳过
function alexaIntent(intent, session, callback) {
var results = "";
var params = {
Key: {
"name": {
S: "aaa"
}
},
TableName: "bbb"
};
console.log('1111111111111111111111111111111');
/* Code from here till.....*/
ddb.getItem(params, (err, data) => {
console.log('inside');
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});
/* .....here is being skipped */
console.log('2222222');
callback(session.attributes,
buildSpeechletResponseWithoutCard("I am the King!!", "", "true"));
}
我是异步编程的新手,我是否缺少基本概念/理解?
我得到的输出是:
1111111111111111111111111111111
2222222
没有被跳过。只是non blocking
。传递给 getItem
的第二个参数 ((err, data) => {...
) 的全部意义在于让您知道一旦执行完毕。此外,您看不到 console.error("Unable to read item...
或 console.log("GetItem succeeded:" ...
的原因是因为您告诉 alexaIntent 在等待 getItem
.
只需将最后一个回调调用移至提供给 getItem
的回调内部:
ddb.getItem(params, (err, data) => {
console.log('inside');
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
callback(session.attributes, buildSpeechletResponseWithoutCard("Error!!", "", "true"));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
callback(session.attributes,
buildSpeechletResponseWithoutCard("I am the King!!", "", "true"));
}
});