当前使用访存的解决方案
我们目前已经实现了该功能,并且运行良好。
function setupCKConnect() {
fetch('ajax/myfile.php?id=1234567')
.then(response => {
response.json()
.then(json => {
function1(json)
.then(data => {
function2()
})
.catch(data => {
setupCKConnect()
});
});
});
};
上述代码的说明
我们为什么需要更改
不幸的是,我们的客户群中似乎有很大一部分使用的设备不支持提取。
因此,我试图使用jQuery.ajax()重写此代码。
我知道这似乎倒退了,但是在这种情况下是必须的。
新代码
下面是我到目前为止的代码。请注意,我不是嫁给jQuery的人,因为这是一个更好的选择。
到目前为止,我有以下代码:
function setupCKConnect() {
$.ajax({
type: "GET",
dataType: 'json',
url: 'ajax/myfile.php?id=1234567'
}).done(function(data) {
function1(data)
}).done(function() {
function2()
}).fail(function(jqXHR, textStatus, errorThrown) {
setupCKConnect()
});
};
新代码存在的问题
新代码实际上除了一点点效果很好
任何人都可以帮助您解释如何在function1()完成之前停止function2()
感谢协助
答案 0 :(得分:-1)
因此,ajax响应返回一个延迟的对象,而不是一个promise。这些看起来很相似,但是将两者混为一谈就可以了。我已经基于js fiddle创建了一个deferred.done documentation来说明我的意思。
我敢猜测function1(json)
返回了一个诺言。推迟的人获得价值(未解决的承诺),并认为“让我们继续前进”,您要么需要异步/等待,要么调用然后在该函数调用上暂停执行。
答案 1 :(得分:-1)
如果修复原始代码的缩进,您将看到问题。对import win32com.client as win32
from win32com.client import constants
def save_as_docx(path):
word = win32.gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Open(path)
doc.Activate ()
new_file_abs = os.path.abspath(path)
new_file_abs = re.sub(r'\.\w+$', '.docx', new_file_abs)
word.ActiveDocument.SaveAs(
new_file_abs, FileFormat=constants.wdFormatXMLDocument
)
doc.Close(False)
save_as_docx(some_path)
的调用不在链接到第一个function2()
的{{1}}中,而是在.then()
的结果上调用。并且.then()
也是从那里而不是function1()
来调用的,因此您不能使用.catch()
来代替。
fetch()
因此,您需要类似地构造jQuery代码。
.fail()
这假设function setupCKConnect() {
fetch('ajax/myfile.php?id=1234567')
.then(response => {
response.json()
.then(json => {
function1(json)
.then(data => {
function2()
})
.catch(data => {
setupCKConnect()
});
});
});
};
与原始版本相似,因此它会返回一个承诺。