单击此代码中的按钮后,我想要
consloe.log("Hello")
打印。
async function remove_tag() {
let pr = new Promise(function(resolve, reject) {
function done() {
let btn = document.getElementById("btn");
function button() {
let tag = document.getElementById("main_1");
tag.remove();
}
btn.addEventListener("click", button)
let msg = "done";
return msg;
}
resolve(done());
})
let wait_promise = await pr;
return wait_promise;
}
remove_tag()
.then((value) => {
console.log("hello")
});
现在你在你的
resolve
定义中立即调用 Promise
,导致 Promise
立即解决。您需要在 button
函数中调用它,以便它在单击时运行。
async function remove_tag(){
let pr = new Promise(function(resolve, reject) {
let btn = document.getElementById("btn");
function button(){
let tag = document.getElementById("main_1");
tag.remove();
let msg = "done";
resolve(msg);
}
btn.addEventListener("click", button);
})
let wait_promise = await pr;
return wait_promise;
}
remove_tag()
.then((value)=>{
console.log("hello");
})
async function remove_tag() {
let pr = new Promise(function(resolve, reject) {
function done() {
let btn = document.getElementById("btn");
function button() {
let tag = document.getElementById("main_1");
tag.remove();
console.log("hello"); // move console.log here
}
btn.addEventListener("click", button);
let msg = "done";
return msg;
}
resolve(done());
});
let wait_promise = await pr;
return wait_promise;
}
remove_tag();
async function remove_tag() {
let pr = new Promise(function(resolve, reject) {
function done() {
let btn = document.getElementById("btn");
function button() {
let tag = document.getElementById("main_1");
tag.remove();
resolve(); // resolve the promise after button click
}
btn.addEventListener("click", button);
}
done();
})
await pr; // wait for the promise to be resolved after button click
}
remove_tag()
.then(() => {
console.log("hello");
});