setTimeout never ends in typescript

huangapple go评论74阅读模式
英文:

setTimeout never ends in typescript

问题

I try to implement a sleep method for a React Native application in Typescript. My implementation is the following:

sleep = (ms: number) => new Promise(r => setTimeout(() => r, ms));

inspired by https://stackoverflow.com/questions/37764665/how-to-implement-sleep-function-in-typescript

And I use it this way in a Component:

getData = async () => {
    this.setState({ isLoading: true });
    await this.sleep(1000);
    const res = await getUser("test") ?? new User("", "", "", null, null, 0, 0);
    this.state.isLoading = false;
    this.setState({ user: res, isLoading: false });
}

But using break points, I noticed that the code doesn't go further than await this.sleep(1000); and my application stays in loading state. What could be the reason for this?

英文:

I try to implement a sleep method for a React Native application in Typescript. My implementation is the following:

sleep = (ms:number) => new Promise(r => setTimeout(() => r, ms));

inspired by https://stackoverflow.com/questions/37764665/how-to-implement-sleep-function-in-typescript

And I use it this way in a Component:

getData = async () => {
    this.setState({isLoading: true});
    await this.sleep(1000);
    const res = await getUser("test") ?? new User("", "", "", null, null, 0, 0);
    this.state.isLoading = false;
    this.setState({user : res, isLoading: false});
}

But using break points, I noticed that the code doesn't go further than await this.sleep(1000); and my application stays in loading state. What could be the reason for this ?

答案1

得分: 1

Here's the translated content:

替换成:

sleep = (ms) => new Promise(r => setTimeout(r, ms));
// 用 `r` 替代 `() => r`

问题在于,在 setTimeout 内部,你当前返回了一个函数 r,而 setTimeout 没有使用它。

setTimeout(() => r, ms)
// 相当于
setTimeout(() => {
    // 这里你什么都没做
    return r; // setTimeout 没有使用这个
}, ms)

这里查看 setTimeout 的文档以了解更多信息。

更新:

  • 修复:No overload matches this call
const sleep = (ms: number) => new Promise<void>((resolve) => {
    setTimeout(() => {
        resolve()
    }, ms);
});
英文:

instead of

sleep = (ms:number) =&gt; new Promise(r =&gt; setTimeout(() =&gt; r, ms));
sleep = (ms) =&gt; new Promise(r =&gt; setTimeout(r, ms));
// replace `() =&gt; r` with `r`

So the problem is, inside setTimeout, instead of execute function r you currently return a function r and setTimeout not using it.

setTimeout(() =&gt; r, ms)
// same as
setTimeout(() =&gt; {
    // you doing nothing here
    return r; // setTimeout not using this
}, ms)

Learn more at document of setTimeout at here

Update:

  • fix: No overload matches this call
const sleep = (ms: number) =&gt; new Promise&lt;void&gt;((resolve) =&gt; {
    setTimeout(() =&gt; {
        resolve()
    }, ms);
});

答案2

得分: 0

Translated:

解决方法如下:

sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms));
英文:

Solved with the following:

sleep = (ms: number) =&gt; new Promise&lt;void&gt;(r =&gt; setTimeout(r, ms));

huangapple
  • 本文由 发表于 2023年5月22日 17:31:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定