【发布时间】:2018-09-22 13:25:53
【问题描述】:
我有这个代码:-
var i = 3
var p = Promise.resolve(i)
while (i > 0) {
(i => {
p = p.then(() => {
return new Promise(function (resolve, reject) {
console.log('start', i)
setTimeout(function () {
setTimeout(() => {
console.log('timeout')
}, 1000);
console.log('end', i)
resolve()
}, 1000)
})
})
})(i)
i--
}
p = p.then(data => console.log('execution ends'))
我得到了这样的输出:-
start 3
end 3
start 2
timeout
end 2
start 1
timeout
end 1
execution ends
timeout
但是,我的预期输出应该是这样的:-
start3
timeout
end3
start2
timeout
end2
start1
timeout
end1
简而言之,正如我预期的输出“超时”日志应该每 2 秒打印一次,其他日志应该每 1 秒打印一次。
【问题讨论】:
-
你可以在你的预期输出中添加时间。我不清楚间隔。看到第一个演示中的时间戳?
-
你的问题有点不清楚。此外 * 我的预期输出看起来像 * 与当前代码的输出相同。请检查并更新问题以获得更清晰的信息
标签: javascript node.js socket.io mean-stack async.js