简体   繁体   English

Promise.then() 不等待 Promise 在一个 Promise 链中的 Resolve()

[英]Promise .then() Not Waiting For Resolve() Of the Previous Promise In A Promise Chain

I am very new to the concept of Promises but I read that this is an efficient way of making functions execute one after another by doing something called Promise chaining.我对 Promises 的概念很陌生,但我读到这是一种通过执行称为 Promise 链接的操作使函数一个接一个地执行的有效方法。

The //RUN ON CLCK: CREATE TABLES code below basically makes two AJAX calls, "Create Database Tables" and "Check Database Tables", so that when I press a button on the webpage, Database Tables will be created on the backend, and then their existence will be checked.下面的 //RUN ON CLCK: CREATE TABLES 代码基本上进行了两次 AJAX 调用,“创建数据库表”和“检查数据库表”,这样当我在网页上按下一个按钮时,数据库表将在后端创建,并且然后将检查它们的存在。

But it doesn't run as intended.但它没有按预期运行。 In many cases, as obseved from the console log, the second function runs (or) finishes first.在许多情况下,从控制台日志中可以看出,第二个 function 首先运行(或)完成。 In any case, that shouldn't happen in a chain.无论如何,这不应该发生在连锁店中。

It looks like the 2nd function is not waiting for a resolve from the 1st function.看起来第二个 function 没有等待第一个 function 的解决。

Please note that my functions have arguments so I can't avoid calling them without parentheses inside the then() as some other articles recommend.请注意,我的函数有 arguments,所以我无法避免在 then() 内调用它们时不带括号,正如其他一些文章所建议的那样。

 $(document).ready(function() { /*PAGE VARS*/ var mainAdmStgChkTblSrvltMsg_elm = document.getElementById('mainAdmStgChkTblSrvltMsg'); var mainAdmStgCrDelTblSrvltMsg_elm = document.getElementById('mainAdmStgCrDelTblSrvltMsg'); var mainAdmStgCrTblBtn_elm = document.getElementById('mainAdmStgCrTblBtn'); /*FN DEF: CHECK TABLES*/ var chkTbl = function(tblNm) { return new Promise(function(resolve, reject) { $.ajax({ type: 'GET', url: '../../../../../../app/TblSrvlt', data: { getType: 'chkTbl', tblNm: tblNm }, success: function(data) { var srvltMsg = data.srvltMsg; var srvltSuccess = data.srvltSuccess; mainAdmStgChkTblSrvltMsg_elm.textContent = srvltMsg; if (srvltSuccess === true) { mainAdmStgChkTblSrvltMsg_elm.setAttribute('class', 'text-success'); } else { mainAdmStgChkTblSrvltMsg_elm.setAttribute('class', 'text-danger'); } /*RETURN RESOLVE FOR PROMISE CHAIN*/ console.log("chkTbl"); resolve(); } }); }); }; /*FN DEF: CREATE TABLES*/ var crTbl = function(tblNm) { return new Promise(function(resolve, reject) { $.ajax({ type: 'POST', url: '../../../../../../app/TblSrvlt', data: { postType: 'crTbl', tblNm: tblNm }, success: function(data) { var srvltMsg = data.srvltMsg; var srvltSuccess = data.srvltSuccess; mainAdmStgCrDelTblSrvltMsg_elm.textContent = srvltMsg; if (srvltSuccess === true) { mainAdmStgCrDelTblSrvltMsg_elm.setAttribute('class', 'text-success'); } else { mainAdmStgCrDelTblSrvltMsg_elm.setAttribute('class', 'text-danger'); } /*RETURN RESOLVE FOR PROMISE CHAIN*/ console.log("crTbl"); resolve(); } }); }); }; /*RUN ON CLCK: CREATE TABLES*/ $(document).on('click', '#' + mainAdmStgCrTblBtn_elm.id, function() { Promise.resolve().then(crTbl("chairs")).then(chkTbl("chairs")).catch(); }); });

You have to pass a function reference to .then() so it can call your function LATER.您必须将 function 引用传递给 .then .then()以便它稍后可以调用您的 function。 Instead, you are calling the functions immediately and passing their return value to .then() which is why they are getting called immediately and not waiting for the prior promise to resolve.相反,您立即调用这些函数并将它们的返回值传递给.then()这就是为什么它们会立即被调用而不是等待先前的 promise 来解析。

Change this:改变这个:

/*RUN ON CLCK: CREATE TABLES*/
$(document).on('click', '#' + mainAdmStgCrTblBtn_elm.id, function() {
    Promise.resolve()
        .then(crTbl("chairs"))
        .then(chkTbl("chairs"))
        .catch();
});

to this:对此:

/*RUN ON CLCK: CREATE TABLES*/
$(document).on('click', '#' + mainAdmStgCrTblBtn_elm.id, function() {
    Promise.resolve()
        .then(function() {return crTbl("chairs")})
        .then(function() {return chkTbl("chairs")})
        .catch(function(err) { console.log(err)});
});

And, you don't actually need the Promise.resolve() at the start of the chain:而且,您实际上并不需要链开头的Promise.resolve()

/*RUN ON CLCK: CREATE TABLES*/
$(document).on('click', '#' + mainAdmStgCrTblBtn_elm.id, function() {
     crTbl("chairs")
        .then(function() {return chkTbl("chairs")})
        .catch(function(err) { console.log(err)});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM