异步编程中的 Deferred 和 Promise 对象及单元测试
1. Deferred 方法概述
在处理异步代码时,我们经常会遇到回调地狱的问题。而使用 Deferred 和 Promise 对象可以很好地避免这一问题。下面我们来看一个 animate 函数的例子:
<!DOCTYPE html>
<html>
<body>
<button id="run-button">Run</button>
<div class="progress"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function animate(milliseconds) {
var deferred = $.Deferred();
setTimeout(function() {
for (var i = 0; i <= 100; i++) {
(function(value) {
setTimeout(function() {
deferred.notify(value);
超级会员免费看
订阅专栏 解锁全文
978

被折叠的 条评论
为什么被折叠?



