XMLHttpRequest Promise,嵌套在一些同步代码中
XMLHttpRequest Promise, nested in some sync code
为什么不推荐同步 xmlhttprequest?
如果浏览器等待数据而不是等待数据,我将不胜感激
打开它以进一步单击而不完成数据查询。
我想写一个一步接一步的程序。
此外,代码的结构应该正确,我想知道如何
我可以在没有 promise-hell 的情况下实现这一点。
是否有任何不是 "deprecated" 的 xmlhttprequest 同步替代方案?
谢谢
托马斯
Why is a sync xmlhttprequest deprecated ?
因为它会阻止 Event Loop 并给您的最终用户带来糟糕的用户体验。
基本上它 "freezes" 正在发生的页面。
I would appreciate if the browser waits for the data
但这不是浏览器的工作方式。幸运的是,通过实践,浏览器 do 的工作方式开始变得非常有意义 - 对于以前主要使用同步 I/O 的语言(如 Python 添加 "promises"也是。
I would like to write a program with just one step after the other.
你可以return responses from asynchronous calls in JavaScript. Using async/await code looks pretty similar and readable. See the async function
mdn page.
Moreover the code should be properly structured and I wonder how I can realize this without the promise-hell.
"promise-hell" 指的是一旦一个函数执行 I/O 它调用的所有其他函数的事实。我不相信这是一件坏事,或者
有什么问题
async function sequence() {
let one = await fetch('/your-endpoint?someParam').then(x => x.json());
// do something with one
let param = one.param;
let two = await fetch('/your-other?param=' + param).then(x => x.json());
// do something with second call
}
sequence();
也就是说,async/await让你可以同步编写异步代码。
不要指望一下子理解所有这些 - 没关系,这需要时间。
为什么不推荐同步 xmlhttprequest?
如果浏览器等待数据而不是等待数据,我将不胜感激 打开它以进一步单击而不完成数据查询。
我想写一个一步接一步的程序。 此外,代码的结构应该正确,我想知道如何 我可以在没有 promise-hell 的情况下实现这一点。
是否有任何不是 "deprecated" 的 xmlhttprequest 同步替代方案?
谢谢 托马斯
Why is a sync xmlhttprequest deprecated ?
因为它会阻止 Event Loop 并给您的最终用户带来糟糕的用户体验。
基本上它 "freezes" 正在发生的页面。
I would appreciate if the browser waits for the data
但这不是浏览器的工作方式。幸运的是,通过实践,浏览器 do 的工作方式开始变得非常有意义 - 对于以前主要使用同步 I/O 的语言(如 Python 添加 "promises"也是。
I would like to write a program with just one step after the other.
你可以return responses from asynchronous calls in JavaScript. Using async/await code looks pretty similar and readable. See the async function
mdn page.
Moreover the code should be properly structured and I wonder how I can realize this without the promise-hell.
"promise-hell" 指的是一旦一个函数执行 I/O 它调用的所有其他函数的事实。我不相信这是一件坏事,或者
有什么问题async function sequence() {
let one = await fetch('/your-endpoint?someParam').then(x => x.json());
// do something with one
let param = one.param;
let two = await fetch('/your-other?param=' + param).then(x => x.json());
// do something with second call
}
sequence();
也就是说,async/await让你可以同步编写异步代码。
不要指望一下子理解所有这些 - 没关系,这需要时间。