为什么将代码包装在resolved prop 中会使同步代码表现得像异步代码?
Promise 内部的循环就像同步代码一样工作,例如:
console.log('im working')
function proLoop(){
return Promise((resolve ,rejects)=>{
for (let i = 0; i < 1000000000000; i++) {}
console.log('loop is done')
})
}
proLoop();
console.log('im working')
因此,即使我们编写像 Promise 一样的代码,它也会获得更多时间并冻结我们的代码。换句话说,它将同步工作。
我找到了解决方案,但为什么它有效?
所以解决方案就是像
这样
return new Promise.resolve().then( ()=>{
for (let i = 0; i < 1000000000000; i++) {}
console.log('loop is done')
})
解决承诺一样扭曲你的代码,但为什么以及如何???
Loop inside of the promise works like sync code, for example:
console.log('im working')
function proLoop(){
return Promise((resolve ,rejects)=>{
for (let i = 0; i < 1000000000000; i++) {}
console.log('loop is done')
})
}
proLoop();
console.log('im working')
So even if we write is like promise it will get more time and freezes our code In other words it will works synchronically.
i find a solution but why it works?
so the solution is just warp your code as like resolved promise
like this
return new Promise.resolve().then( ()=>{
for (let i = 0; i < 1000000000000; i++) {}
console.log('loop is done')
})
but why and how???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里您需要了解一些事情:
Promise 不会使某些东西异步 - 它们是已经异步的东西的通知机制。 Promise 会通知您已经异步操作的成功或失败。
promise构造函数的回调函数同步调用;它被同步调用以启动已经异步的操作。
在您的情况下,promise 构造函数包含同步代码;这就是它阻止其他代码的原因。
将循环移至
then
的回调函数内会异步执行循环,因为then
的回调函数是异步调用的。注意:尽管
then
方法的回调函数是异步调用的,但一旦开始执行,在回调内的同步代码执行完毕之前,不会再执行其他代码。Couple of things you need to understand here:
Promises don't make something asynchronous - they are a notification mechanism for something that's already asynchronous. Promises notify you of the success or failure of an operation that's already asynchronous.
Callback function of the promise constructor is called synchronously; it is called synchronously to start an operation that's already asynchronous.
In your case, promise constructor contains synchronous code; this is why it blocks other code.
Moving the loop inside the callback function of
then
executes the loop asynchronously because the callback function ofthen
is invoked asynchronously.Note: Even though the callback function of
then
method is invoked asynchronously, once it starts executing, no other code will execute until the synchronous code inside the callback is executed completely.在这两种情况下,
for
循环都会阻塞主事件循环。不同之处在于for
循环何时开始。在第一种情况下,传递给 new Promise 的函数会立即执行。因此,它会阻止创建它的函数的其余部分。
在第二种情况下,传递给 then() 的函数会排队等待下次事件循环空闲时运行。因此,创建它的函数首先完成,然后运行 for 循环。 (如果这是目标,那么它是一种非常不直观的方法,并使用
setImmediate
(在浏览器中)或 Node 的process.nextTick()
会更简洁。)如果您想将
for
循环完成的工作从主事件循环中分流出来,那么它没有阻止任何您将使用 Web Worker 的内容,或者<一href="https://nodejs.org/api/worker_threads.html" rel="nofollow noreferrer">工作线程 取决于您使用的 JavaScript 运行时。In both cases, the
for
loop will block the main event loop. The difference is when thefor
loop starts.In the first case, the function passed to
new Promise
is executed immediately. It therefore blocks the rest of the function that creates it.In the second case, the function passed to
then()
is queued up to run next time the event loop is free. The function that created it therefore finishes first and then thefor
loop runs. (If that's the goal, then its a pretty unintuitive approach, and usingsetImmediate
(in browsers) or Node'sprocess.nextTick()
would be neater.)If you wanted to shunt the work done by the
for
loop off the main event loop so it didn't block anything you would use a Web Worker or Worker thread depending on what JavaScript runtime you were using.