dev-resources.site
for different kinds of informations.
My mistake with resolve and return resolve
Published at
5/19/2024
Categories
javascript
webdev
beginners
learning
Author
amythical
TLDR : Just calling resolve does not stop further code execution, return resolve is the right way to stop further code execution.
Lets see this example.
function fun1(){
returns new Promise(){
console.log("Before calling fun2");
fun2().then(response){
if(response == 1234){
resolve(response);
}
console.log("After resolve");
}
}
}
Now if you run this, the console prints
Before calling fun2
After resolve
But ... I had called resolve so the code at "After resolve" should not have been reached.
WRONG!
Correct use
fun2().then(response){
if(response == 1234){
return resolve(response);
}
console.log("After resolve");
}
Now it works as expected and returns where intended stopping further code execution.
This was a small oversight but it leads to unexpected behaviour in the logic flow if ignored.
Just sharing my experiences, small things that will save time, sweat and hair on the head.
Cheers.
Articles
12 articles in total
A rec, CNAME, TXT recs
read article
Troubleshooting slow page loads
read article
Call C binary from javascript WITHOUT process.exec
read article
Unresponsive UI and a busy main thread?
read article
My mistake with resolve and return resolve
currently reading
AWS Cognito terms that confused me
read article
Nodejs axios certificate error
read article
Webpack4 fix for .mjs files
read article
Lazy loading components in ReactJS
read article
Lazy loading images using JS & ReactJS
read article
Simple function to print table in browser console
read article
New to DEV.to - About me
read article
Featured ones: