这是我当前的代码
function getInput() {
Swal.fire({
input: "text",
}).then((result) => {
if (result) {
return(result.value);
});
}
function main() {
const test = getInput();
}
我知道这是不正确的,因为它返回未定义。我曾尝试寻找一堆解决方案,但我总是不确定,或者承诺本身返回,不知道我应该如何获得价值。
我想做的是以某种方式返回SweetAlert2输入的值,以便可以在其他函数中使用它。
返回值时不确定使用程序callback
作为程序的理想用例。
在then
处理程序中,将调用回调函数,并且将input
作为参数传递。
function getInput(cb) {
Swal.fire({
input: "text",
}).then((result) => {
if (result) {
return cb(result.value);
});
}
}
function main(input) {
console.log(input);
}
getInput(main)