1.var let
for (var i = 0; i < 5; i++) {
setTimeout(()=> {
console.log(i)
}, 1000)
}
console.log(i)
// 先打印后面的console:5, 1秒后连续输出5个5
for (let i = 0; i < 5; i++) {
setTimeout(()=> {
console.log(i)
}, 1000)
}
console.log(i)
// 先打印后面的console:报错, 1秒后连续输出0 1 2 3 4
2.promise
console.log('main-1')
function foo(param){
var p = new Promise(function(resolve, reject) {
console.log('promise-1')
if (param === 1) {
resolve('1')
} else {
reject('2')
}
console.log('promise-2')
})
return p
}
console.log('main-2')
foo(1).then(function(value) {
console.log('then-1')
})
console.log('main-3')
// 运行结果是
// main-1
// main-2
// promise-1
// promise-2
// main-3
// then-1