What's Ideal situation for using promises

I have been learning promises, i know it can be used on asynchronous code, like for API fetching ,etc. When we create our own function that returns promises, How to determine which code can be considered as asynchronous, can anyone give clarity on this.
2 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
13eck
13eck3y ago
Promises are basically used when the thing it's doing can take some time and you don't want to wait. API fetching is a big one, but also for things like accessing the local DB (IndexedDB) or on Node handling http requests with a server. Any time you want to do other things at the same time or the result of the function isn't dire and can be "put off until later". In such a case, your synchronous code will finish running before the promise happens. https://javascript.info/promise-basics Task queues are also great for promises: you return a Promise for completion of the queue, add the (Promiseified) task to the queue and when the task gets handled the promise resolves (or rejects, if Something Went Wrong™️) and the final result can be taken care of.
Want results from more Discord servers?
Add your server