What is the point of the concept ‘promise’
I been watching ‘Promise’ videos and still dont understand the concept. When is it most relevant to use it?
6 Replies
i saw an example where they use an if statement in it and im wondering why dont they just use the if statement without the promise
Promises are functions that will eventually resolve to a value (or fail to resolve to a value) but how long it takes to do so is never known.
Promises are for things like HTTP requests (using
fetch()
) where network latency makes it impossible to know when/if it'll fulfill. Promises also give control back to the JS runtime so while it's waiting the rest of your code can still run.For more info on promises: https://javascript.info/async
Also, see this recent post:
https://discord.com/channels/436251713830125568/1022568654153465968
Promises also give control back to the JS runtime so while it's waiting the rest of your code can still run.Just want to emphasize, this is the main reason we use promises in the first place. We don't want our code to idle doing nothing while it waits for the result of a network request to come back. This could be 30, 100 or 2000 milliseconds which may not sound like much but it's quite a lot.
Ok now I get it thank you for the explanation