event-loop

Why does this line of code with 'await' trigger microtask queue processing?

∥☆過路亽.° 提交于 2019-12-03 07:30:01
问题 The following quotes are my primary references for understanding microtask queue processing: Microtasks (which promises use) are processed when the JS stack empties. - Jake Archibald That doesn't make sense to me. One go-around of the event loop will have exactly one task being processed from the macrotask queue (this queue is simply called the task queue in the WHATWG specification). After this macrotask has finished, all available microtasks will be processed, namely within the same go

JavaScript - When exactly does the call stack become “empty”?

流过昼夜 提交于 2019-12-02 22:45:20
I've read several posts/SO threads on event loop, and according to MDN's article , When the stack is empty, a message is taken out of the queue and processed. As a JS novice, what I'm still confused about is -- when exactly does the call stack become "empty"? For example, <script> function f() { console.log("foo"); setTimeout(g, 0); console.log("foo again"); } function g() { console.log("bar"); } function b() { console.log("bye"); } f(); /*<---- Is the stack empty here? */ b(); </script> The correct order of execution is foo - foo again - bye - bar . But today I started thinking: isn't the

Why does this line of code with 'await' trigger microtask queue processing?

谁都会走 提交于 2019-12-02 22:20:25
The following quotes are my primary references for understanding microtask queue processing: Microtasks (which promises use) are processed when the JS stack empties. - Jake Archibald That doesn't make sense to me. One go-around of the event loop will have exactly one task being processed from the macrotask queue (this queue is simply called the task queue in the WHATWG specification). After this macrotask has finished, all available microtasks will be processed, namely within the same go-around cycle. - Stack Overflow Now, regarding line 9 (**) in the following snippet: From stepping through

Python3 Asyncio call from Flask route

徘徊边缘 提交于 2019-12-02 16:18:08
I want to execute an async function everytime the flask route is executed. Currently my abar function is never executed. Can you tell me why? Thank you very much: import asyncio from flask import Flask async def abar(a): print(a) loop = asyncio.get_event_loop() app = Flask(__name__) @app.route("/") def notify(): asyncio.ensure_future(abar("abar"), loop=loop) return "OK" if __name__ == "__main__": app.run(debug=False, use_reloader=False) loop.run_forever() I tried it also to put the one blocking call in a seperate thread. But it is still not calling the abar function. import asyncio from

Javascript callback function not executing as intended

。_饼干妹妹 提交于 2019-12-02 10:33:19
问题 According to this stackoverflow answer, functions passed as parameters are always callbacks, even if the intention is that the function is called synchronously... and I've learned the event loop mechanism which basically says that call back functions are push into a waiting queue and executed after synchronous code finishes(stack empty), however in the following code function myfun(a, b, callback) { console.log(a); callback(); console.log(b); } function cb() {console.log('hi')} myfun(1, 2, cb

Javascript callback function not executing as intended

走远了吗. 提交于 2019-12-02 06:07:29
According to this stackoverflow answer , functions passed as parameters are always callbacks, even if the intention is that the function is called synchronously... and I've learned the event loop mechanism which basically says that call back functions are push into a waiting queue and executed after synchronous code finishes(stack empty), however in the following code function myfun(a, b, callback) { console.log(a); callback(); console.log(b); } function cb() {console.log('hi')} myfun(1, 2, cb) // result: 1 hi 2 the result is not 1 2 hi as I expected, from which I infer that only callback

javascript || Angular2/6: Calling setInterval multiple times but last timerId is not stopping even though clearInterval called

怎甘沉沦 提交于 2019-12-02 05:18:19
Requirement: User scans multiple job numbers, for each job number , I need to call one API and get the total job details and show it in a table below the scanned text box. User don't want to wait until API call finishes. He will scan continuously irrespective of details came or not. What I have done: I have taken one variable jobNumberList which stores all the job numbers the user scanned I am continuously calling the API, with those job numbers. When API, gives response , then I am adding to the table I created one method called m1() m1() => this method will compare the jobNumberList with the

Does the JS callstack always have at least one frame?

只谈情不闲聊 提交于 2019-12-01 18:48:51
I've recently seen a presentation on the JS event loop which is, frankly, brilliant, but I have a lingering question now about the JS call stack. If you think about the global execution context as, say, main(), is main() never resolved? My reasoning here is that, if it were, then the JS program would be complete, and no callbacks would happen. --edit My primary interest here is how the call stack is represented, in relation to the callback queue. If the event loop is said to wait until the call stack is empty before pushing new frames onto the stack, then the loop would be waiting until the

Async call generates “ Error: Can't wait without a ”fiber\", even with _wrapAsync

我的未来我决定 提交于 2019-12-01 14:40:28
I've been having a problem using an RSS parser in meteor. It's an async call, so it needs ot be wrapped, however it still doesn't seem to work. I presume this is because the anonymous on('readable' function is outside the fiber, but I can't see how to resolve it. var FeedParser = Meteor.require('feedparser'); var request = Meteor.require('request'); function getBlog(url, parameter, id){ request(parameter) .on('error', function (error) { console.error(error); }) .pipe(new FeedParser()) .on('error', function (error) { console.error(error); }) .on('readable', function () { var stream = this, item

Async call generates “ Error: Can't wait without a ”fiber", even with _wrapAsync

霸气de小男生 提交于 2019-12-01 12:39:05
问题 I've been having a problem using an RSS parser in meteor. It's an async call, so it needs ot be wrapped, however it still doesn't seem to work. I presume this is because the anonymous on('readable' function is outside the fiber, but I can't see how to resolve it. var FeedParser = Meteor.require('feedparser'); var request = Meteor.require('request'); function getBlog(url, parameter, id){ request(parameter) .on('error', function (error) { console.error(error); }) .pipe(new FeedParser()) .on(