[英]How can I await promise().done() with typescript?
I know how to create a standard promise and use async/await to it but how can I await jquery promise().done() with typescript? 我知道如何创建一个标准的Promise并对其使用async / await,但是我该如何等待带有TypeScript的jquery promise()。done()?
Current: 当前:
$('#sun').hide('slide', {direction:'right'}, 200).promise().done(function () {
$('#rain').show('slide', {direction:'left'}, 200).promise().done(function () {
console.log('ok!');
});
});
I would like to do in this way but it does not work: 我想用这种方式做,但是不起作用:
await $('#sun').hide('slide', {direction:'right'}, 200).promise().done();
await $('#rain').show('slide', {direction:'left'}, 200).promise().done();
console.log('ok!');
Try removing the done()
call from the ends of your promise()
calls. 尝试从您的promise()
调用的末尾删除done()
promise()
调用。 Without specifically looking into the jQuery promise implementation I would imagine calling done()
without an argument returns undefined
or an otherwise unawaitable result, so you're trying to await undefined
. 如果不专门研究jQuery Promise实现,我会想象没有参数的调用done()
返回undefined
或其他无法等待的结果,因此您正在尝试await undefined
。
Try awaiting the promise()
call instead: 尝试等待promise()
调用:
await $('#sun').hide('slide', {direction:'right'}, 200).promise();
await $('#rain').show('slide', {direction:'left'}, 200).promise();
console.log('ok!');
A jquery promise works in the same manner as a vanilla promise 一个jQuery的承诺,以相同的方式工作作为香草承诺
An example on how it works, convert this: 有关其工作方式的示例,请转换为:
var div = $( "<div>" ); div.promise().done(function( arg1 ) { // Will fire right away and alert "true" console.log( this === div && arg1 === div ); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
to this: 对此:
var div = $( "<div>" ); async function run(){ const arg1 = await div.promise(); console.log(arg1 === div ); } window.onload = run;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Solution: 解:
await $('#sun').hide('slide', {direction:'right'}, 200).promise();
await $('#rain').show('slide', {direction:'left'}, 200).promise();
console.log('ok!');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.