Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

response 返回不支持异步逻辑 #48

Open
dangjingtao opened this issue Oct 6, 2021 · 2 comments
Open

response 返回不支持异步逻辑 #48

dangjingtao opened this issue Oct 6, 2021 · 2 comments

Comments

@dangjingtao
Copy link

module.exports = [
  {
    url: "/api/xxx/query",
    method: "get",
    response: async (req, res) => {
      const params = req.body;

      const list = await getFile('./xxx/extend.js');

      const ret = {
        code: 'SUCCESS',
        data: {
          list
        },
        message: '查询接口成功',
        success: true,
      }

      return ret
    },
  },
]

以上代码不支持从持久储存(extend.js文件)中异步读取接口信息。返回为{}
看源码发现requestMiddleware中定义返回部分,尝试对response定义await:

const mockResponse = isFunction(response) ? **await** response.bind(self)({ url: req.url, body, query, headers: req.headers }) : response;

可以达到预期返回,求教这样修改是否有影响?如无影响,是否考虑在下个版本中支持异步写法

@Broderick890
Copy link

+1

condorheroblog added a commit to condorheroblog/vite-plugin-fake-server that referenced this issue Oct 16, 2023
@luck2011
Copy link

luck2011 commented Dec 5, 2024

+1 同时需要加sleep() 来模拟真实网络状态

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants