3 ms·
Okay, in python this is: print "first" print "second" I mean...imagine explaining your example to somebody. That's difficult for some programmers t
by blhack 8y ago
Okay, in python this is:
print "first"
print "second"
I mean...imagine explaining your example to somebody. That's difficult for some programmers to fully understand.
- joshuamorton 8y agoNo. In python(3.7+) this is import asyncio async def first(): print("first") async def second(): print("second") async def _(): await first() await second() asyncio.run(_()) prior to python3.7, python didn't have similarly clean asynchronous programming tools. With python 3.5+, you could use the old event loop syntax [1] to do it, and prior to that, you needed to use `yield` and `yield from` for similar semantics. If you want synchronous stuff in JS, this suffices: console.log('first'); console.log('second'); EDIT: The problem that JS had (prior to promises in ES5) was that the only way to do deferred/asynchronous things (like "run this after I get data from a network call") was to provide a callback, something like function(resource_url, callback) { data = get(resource_url); callback(data); } This gets very trick very fast if you want to have chained calls (imagine that `callback` also conditionally requests a resource, and you want to do something with that resource, and based on that you may want to redraw the DOM and then...). There wasn't a good pattern for describing that in JS. The "common" pattern was to just have callbacks within callbacks, and unlike in python you'd often use anonymous functions, so you end up with nested anonymous functions which inverts the way you think, it's really hard to grok. Promises and async/await linearize that, but that's a problem that python never really had to address because up until very recently, python didn't get used for async stuff. [1]: https://docs.python.org/3.6/library/asyncio-task.html#example-hello-world-coroutine https://docs.python.org/3.6/library/asyncio-task.html#exampl...