var doExe = function (a , callback) {
setTimeout(function () {
console.log(a)
if(callback) callback
}, 100 * Math.random())
}
var aTest = function (num) {
for(var i=0 ; i<num ; i++){
doExe(i)
}
}
aTest(10)
需求:
doExe是带个callback的函数,要求执行aTest(10)输出0123456789
注意: guaTest函数可以随意改动,但是doExe 不能改动(不知道有没有记错doExe的代码,如果可以改直接用promise包一下解决了)
目前方案(目前我自己尝试出可以用递归解决,如下面)
var doExe = function (a, callback) {
setTimeout(function () {
console.log(a)
if (callback) {
callback()
}
}, 100 * Math.random())
}
var max = 9
var aTest = function (index) {
doExe(index, () => {
index < max && aTest(++index)
})
}
aTest(0)
那个面试官是用队列+doExe.apply解决了,当时没看懂,请问这个怎么做的,或者说请问有什么其他解决方案