对比一下setTimeout和Promise的实现方法,promise感觉更高大上。
setTimeout 实现
setTimeout(function() {
console.log('1s')
setTimeout(function() {
console.log('2s')
}, 1000)
}, 1000)
Promise 实现
const wait = () => new Promise((resolve, reject) => {
setTimeout(resolve, 1000)
})
wait().then(() => {
console.log('1s')
return wait()
})
.then(() => console.log('2s'))
- 萌面大虾,每日更新js代码技巧:想要了解更多请关注:https://github.com/57abc/dev