V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Newyorkcity
V2EX  ›  问与答

在 chrome 的 devtools 的 console 里,有啥方法可以假装发出了一个 ajax 吗?

  •  
  •   Newyorkcity · 2022-04-08 20:07:56 +08:00 · 396 次点击
    这是一个创建于 762 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想研究一下 await async 的事儿,所以需要一个类似于 ajax 那样过一段时间有返回值的调用。

    但又只是想在 chrome 的 devtools 的 console 里浅尝辄止,jquery 都懒得上,且想要弄自由控制这个调用返回值的时间。

    想问下有什么方法可以假装、模拟这个效果?我印象里 setTimeout 应该可以,但我刚才搜了一下,都是跟 Promise 一起用。

    !!不要 Promise !!!

    谢谢
    noe132
        1
    noe132  
       2022-04-08 21:09:30 +08:00   ❤️ 1
    async 函数的返回值就是 Promise
    await 等同于 Promise.then
    “The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.”
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#return_value

    你要的函数:
    const sleepWith = (value, time) => new Promise(rs => setTimeout(() => rs(value), time))
    Sunzehui
        2
    Sunzehui  
       2022-04-09 06:29:20 +08:00
    你可以在 nodejs 写个传入多少延时参数 sleep 后就 return 的 API ,前端 fetch 调用不用导入 jquery
    或者自己实现一个任务队列
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1910 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:25 · PVG 00:25 · LAX 09:25 · JFK 12:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.