+ 1
What exactly happens when you write promise.this(function())?
I never really understood promises completely, I do know how to use them but I can't really understand them. I thought that the function used inside this() is assigned to the resolve parameter of the promise like a function callback, but I guess that's not true since adding more than one parameter to resolve doesn't work. So what exactly happens when I write example.this(function); ? Am I overthinking it? Is it just supposed to work the way it works, if the function inside this() has a parameter is simply takes the value inside resolve() and there is nothing more happening behind the scenes? https://code.sololearn.com/WEUmAyMfzgxU/?ref=app
3 Antworten
+ 5
Karak10 maybe this reference will help you understand atleast the promise part of the promise.this(function())
https://javascript.info/promise-basics
+ 3
Karak10
so when you put a function in then(), itll execute that when the promise resolves, which you do by calling resolve()
And you can only resolve with one result, not two!
Which is the point of the promise, for asynchronous execution
and if you are talking about then() "then"
~then() executes when the promise resolves successfully!
+ 1
BroFar there is one last thing I don't seem to understand, why exactly are promises so useful? They simply return a function if a condition is met or another if not, something we could do anyway using if else stataments. The only use for them I see is when you want to do function callbacks nesting, so that one promise is checked after another, without creating this weird function callbacks hell, so they basically simplify function call backs syntax? People say there are three states for a promise, pending, fulfilled or failed, I don't really understand the pending state, because when a promise is called using then(), it checks if the promise is fulfilled or not and that's all, if for example I wanted it to fulfill after a variable x becomes 10, and x became 10 after I called the promise using then() the promise wouldn't fulfill, not if I hadn't made it loop the code block using setInterval to repeatedly check if x is 10. It is pending only if I have set a timeout or a loop else it checks once and that's all.