gpt4 book ai didi

javascript - 为什么我的 promise 的 'resolve' 在函数结束执行之前被发送?

转载 作者:行者123 更新时间:2023-11-30 19:24:09 25 4
gpt4 key购买 nike

我有一个函数(内部包含 promise ,因此它本身同步运行)似乎在我的主代码中异步运行。无论我如何格式化我的 promise ,似乎在函数结束执行之前发送了解决方案:

这个问题在逻辑上也是递归的,因为如果我尝试在 nameExists 函数周围添加另一个 promise(在这个 promise 内),然后将 resolve 放在 'then' 中,我就会遇到与嵌套 resolve 相同的问题...

    document.getElementById("config-select").addEventListener("input", function(){
//check if the doc name exists: returns doc id
//promise that doc_obj is created before moving on
let doc_obj = {};
let promise = new Promise(function (resolve, reject) {
let doc_name = document.getElementById("config-select").value;
doc_obj = nameExists(doc_name);
resolve('done'); //this executes BEFORE nameExists is done processing...bringing back the original asynch issue i was trying to fix in the first place...
});
promise.then(function (result) {
alert("then: "+doc_obj);
if(doc_obj.bool === true){//it does exist:
alert("replacing id");
document.getElementById("config-select").setAttribute("doc-id", doc_obj.id);
}
else{//it doesn't:
alert("resetting id");
document.getElementById("config-select").setAttribute("doc-id", "");
}
}
);

});

nameExists 函数:

//check if the name in config-select is an existing doc (assumes name is a unique document field)
const nameExists = function(name){
//get all docs
localDB.allDocs({include_docs: true}).then(function (result) {
//return object set to default state if no match is found
let doc_obj = {bool: false, id: ""};
alert("Entering the match checker...");

for(let i =0; i<result.total_rows; i++) {
if(result.rows[i].doc.name == name){
alert(result.rows[i].doc.name);
alert(name);
doc_obj.bool = true;
doc_obj.id = result.rows[i].doc._id;
//found a match
break;
}
}
//return the result
alert("returned obj.id: "+doc_obj.bool);
return doc_obj;

}).catch(function (err) {console.log(err);});
};

理想情况下,在评估我的“if 语句”之前,我希望使用来自 nameExists 函数的数据填充 doc_obj 或某些返回值对象。我如何格式化我的 promise/resolve 语句来实现这一点?

最佳答案

你应该放弃那个 new Promise - 它不会改变你是否能够等待 nameExists 的结果。您将需要返回 then()nameExists 函数中创建的 promise :

function nameExists(name) {
return localDB.allDocs({include_docs: true}).then(function (result) {
//^^^^^^
for (let i =0; i<result.total_rows; i++) {
if (result.rows[i].doc.name == name){
return {bool: true, id: result.rows[i].doc._id};
}
}
return {bool: false, id: ""};
});
// ^ don't catch errors here if you cannot handle them and provide a fallback result
}

然后你可以在你的事件监听器中等待它:

document.getElementById("config-select").addEventListener("input", function() {
const doc_select = document.getElementById("config-select");
const doc_name = doc_select.value;
// check if the doc name exists: returns doc id
nameExists(doc_name).then(function(doc_obj) {
//^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^
console.log("then", doc_obj);
if (doc_obj.bool) { // it does exist:
alert("replacing id");
} else { // it doesn't:
alert("resetting id");
}
doc_select.setAttribute("doc-id", doc_obj.id); // id is "" when it doesn't exist
}).catch(function (err) {
console.log(err);
})
});

关于javascript - 为什么我的 promise 的 'resolve' 在函数结束执行之前被发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57101033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com