简体   繁体   English

为什么我的 settimeout 没有在导入的模块中触发?

[英]Why isn't my settimeout firing inside an imported module?

I have a Vue component which imports a module as so:我有一个 Vue 组件,它这样导入一个模块:

import { sendMsg } from './assets/modules/messages'

sendMsg looks like this (simplified for brevity). sendMsg看起来像这样(为简洁起见进行了简化)。 There's a setTimeout() to add in an artificial (human-like) delay so that the system doesn't reply instantaneously.有一个setTimeout()来添加一个人为的(类似人类的)延迟,这样系统就不会立即回复。

export const sendMsg = async msg => {
    console.log(1); //happens
    await new Promise(res => setTimeout(res, 1000));
    console.log(2); //doesn't happen
}

...but nothing after the timeout fires, because the timeout doesn't resolve. ...但是在超时触发后什么也没有,因为超时没有解决。

If I move all the code into the component directly, rather than calling it in from a module, it works fine.如果我将所有代码直接移动到组件中,而不是从模块中调用它,它就可以正常工作。 I've searched for "javascript interval not firing inside module" but not found anything.我搜索了“javascript interval not firing inside module”但没有找到任何东西。 What am I missing?我错过了什么?


Update: This happens only when called by App.vue .更新:这在被App.vue调用时发生。 If I import/call the function from a child component, it works fine...如果我从子组件导入/调用 function,它工作正常......

I think you have not read documentation about javascript. It's asynchronous and it will not wait for the event and continue the process.我认为您还没有阅读有关 javascript 的文档。它是异步的,不会等待事件并继续处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM