Promise延时取消函数

2022-03-02202次阅读javascript
Promise可以延时也可以取消的函数
const delayedTime = (duration: number)=>{
    let timeout: NodeJS.Timeout;
    return {
        promise: function (): Promise<void> {
            return new Promise((resolve) => {
                timeout = setTimeout(() => {
                    console.info('timeout complete');
                    resolve();
                }, duration);
            });
        },
        onCancel: function () {
            clearTimeout(timeout);
            console.info('timeout cleared');
        },
    };
};
const delayHander = delayedTime(3000);
// await delayHander.promise();
// delayHander.onCancel();

 

上一篇: TypeScript报错TS2339: Property 'token' does not exist on type 'default'  下一篇: react-router不同路由使用相同组件无法渲染问题  

Promise延时取消函数相关文章