function a (){
let a = 0
a = 1
let loader = setInterval(function () {
abc ()
clearInterval(loader)
}, 1000)
function abc (){
a +=1
}
return a
}
我的预想是 return 的 a 为 2,但是现在 return 的结果依旧是 1,请问这个该怎么解决啊
1
lianyue 2021-08-01 19:00:30 +08:00
返回的时候 setInterval 里面的函数并没被执行
|
3
lianyue 2021-08-01 19:20:03 +08:00
async
await promise 自己了解 |
5
rabbbit 2021-08-01 19:24:08 +08:00
const foo = () => {
let bar = 1 return new Promise(resolve => { setTimeout(() => resolve(++bar), 3000) }) } (async() => console.log(await foo()))() |
8
rabbbit 2021-08-02 15:00:35 +08:00
const foo = () => {
let bar = 1; return new Promise((resolve) => { setTimeout(() => resolve(++bar), 3000); }); }; (() => { foo().then((bar) => { console.log(bar) }) })(); const foo = (callback) => { let bar = 1; setTimeout(() => { bar++; callback?.(bar) }, 3000); }; (() => { foo((bar) => console.log(bar)) })(); |