【发布时间】:2021-07-13 09:12:02
【问题描述】:
我的问题是,您如何覆盖超出此范围的异步函数中的变量? 我读到here 说问题是缺少回调。添加回调后,更改它的范围之外的变量(但变量本身仍然在正确的范围内)返回“未定义”。我做错了什么?
测试:
const savedVariableCallback = (variable) =>
console.log(`Variable saved ${variable}`);
describe(() => {
...
it("Sample input type", () => {
let fixValue;
cy.fixture("example.json").then(({ email }) => {
actionsPage
.selectSampleInput()
.then((input) => {
checkAmountOfElements(input, 1);
checkVisiblity(input);
})
.type(email)
.then((input) => {
checkIfValue(input, email);
fixValue = "Nice work";
savedVariableCallback(fixValue);
});
});
cy.log(`fixValue is: ${fixValue}`);
});
})
我希望第一个日志显示Variable saved Nice work,第二个日志显示fixValue is: Nice work 变量。但是现在,我进入第一个日志Variable saved Nice work,但在第二个日志中我得到undefined。
我希望在 it() 方法范围内可以访问该变量。
【问题讨论】:
标签: javascript asynchronous callback cypress