4 ms·
> Promises are so easy to understand. I still don't understand promises in JavaScript. I got my head around the convoluted constructor (a function whose argume
by pansa2 6y ago
> Promises are so easy to understand.
I still don't understand promises in JavaScript. I got my head around the convoluted constructor (a function whose argument is a function whose arguments are two functions), but I can't make sense of chaining and the "promise resolution procedure" [0].
However I've looked into async-await in Python and it seems straightforward. Doesn't `await foo()` just mean "suspend execution here and don't run any more code in this function until foo is done"?
[0] https://promisesaplus.com/#the-promise-resolution-procedure https://promisesaplus.com/#the-promise-resolution-procedure
- vorticalbox 6y agoYes and that's what a promise is in a javascript. When you call await on a function it gets put into the event loop and when it's read the processing in the function will resume.
- tehlike 6y agoIn recent versions of javascript, you can use almost the same async/await concept. It's the same thing, it's just that original syntax of promise is a bit too verbose.
- crubier 6y agoAsync await in python and in JS work exactly the same way (except that you have to explicitly launch an event loop in python). I have developed a library that makes heavy uses of promises and async generators, and the code for the python and JS version are extremely similar. Like similar enough that an automatic translation could be done. And yes, forget about .then and other old school stuff like that in JS. Just use async await to deal with promises.