我的意图是调用了cheerio.load(),并且使响应混乱。 Google助手不断告诉我,即使稍后在我有回复的代码中也没有设置任何回复。控制台还告诉我没有将异步调用返回给处理程序,我相信它是cheerio.load()。无论如何,我可以解决此问题,以便它继续在代码底部寻找正确的conv.ask吗?它仍然继续向下运行,直到显示console.log(map)。感谢您的帮助!
func createTreatNode() {
let atlas = SKTextureAtlas(named: "Treat")
let q1 = atlas.textureNamed("treat1.png")
let q2 = atlas.textureNamed("treat2.png")
let q3 = atlas.textureNamed("treat3.png")
let treatAnimation = [q1, q2, q3]
let treat = SKSpriteNode(texture: q1)
treat.position = CGPoint(x: 1000, y: 150)
print("treat position \(treat.position)")
treat.size = CGSize(width: 60, height: 50)
treat.name = "Treat"
let animate = SKAction.animate(with: treatAnimation, timePerFrame: 0.2)
let repeatAction = SKAction.repeatForever(animate)
treat.run(repeatAction)
let body = SKPhysicsBody(rectangleOf: treat.size)
body.usesPreciseCollisionDetection = true
body.affectedByGravity = false
body.collisionBitMask = 0x7FFFFFFF
body.contactTestBitMask = 0x80000000
treat.physicsBody = body
addChild(treat)
}
});
答案 0 :(得分:1)
我已修改您的代码以返回Promise。检查是否适合您。
app.intent("late drop", (conv, {
term,
year
}) => {
return new Promise(function (resolve, reject) {
var date = new Date();
var month;
if (term == null) {
month = date.getMonth();
if (month >= 9 && month <= 12) {
term = "fall";
//console.log("fall")
} else if (month >= 1 && month <= 5) {
term = "spring";
//console.log("spring")
} else {
term = "summer";
//console.log("summer")
}
}
if (year == null) {
yearDig = date.getFullYear();
year = yearDig;
//console.log(year)
}
var strYear = year.toString();
var semester = term + strYear.substr(2);
const options = {
uri: `https://www.registrar.psu.edu/academic_calendar/${semester}.cfm`,
transform: function (body) {
return cheerio.load(body);
}
};
rp(options)
.then(($) => {
let map = {};
let columnOne = [];
let columnThree = [];
$('table').find('tr td:nth-child(1)').each(function (index, element) {
columnOne.push($(element).text());
});
$('table').find('tr td:nth-child(3)').each(function (index, element) {
columnThree.push($(element).text());
});
columnOne.forEach((item, i) => {
map[item] = columnThree[i];
});
console.log(map);
date = map["2Late Drop Begins"];
conv.ask("The late drop period begins on " + map["2Late Drop Begins"])
resolve()
})
.catch((error) => {
console.log(error);
conv.ask("An error occured, please try again.");
reject()
})
});
});
希望有帮助!
答案 1 :(得分:1)
问题不在于欢乐。
您似乎正在使用request-promise
或request-promise-native
进行HTTP调用。这将执行异步操作,该操作将返回Promise(如您使用.then()
和.catch()
所证明。
由于执行异步操作的Intent处理程序必须返回一个Promise,因此您只需返回rp / then / catch链返回的那个。像更改此行这样的东西应该起作用:
return rp(options)