闭包和时间戳实现发起多个异步请求永远返回最新结果

var b = null;
var data = 0;
function a() {
var t = b = new Date().getTime();
data += 1;
return new Promise(function(r) {
setTimeout(function() {
r(t, data);
}, 2000);
})
}

function p() {
a().then((t, d) => {
if (t === b) {console.log(d)}
})
}

  

猜你喜欢

转载自www.cnblogs.com/youyouluo/p/12043504.html