Data Scientist Learning JS: Promises and resolve()
Context: I'm a data scientist/analyst (in Python and R) learning development from scratch. Inevitably, I am learning these through the lens of what I already know. If you have a similar background and are a beginner developer, I hope these analogies help! Any comments, especially if you spot any misunderstanding, are appreciated. Commenting is caring <3 Motivation: I was building a mock data…
A data scientist transitioning to development from Python and R shares their experience learning JavaScript, specifically focusing on Promises and the resolve() method. To illustrate the concept, they created a mock data layer for a fitness social app, simulating network requests for fetching new posts.
The function fakeFetchPosts() returns a Promise that resolves with an array of mock posts after a 2-second delay. The main() function logs "Fetching...", then awaits the resolution of fakeFetchPosts() using the await keyword. The fetchedPosts are then logged, followed by "Sync code ran".
The author initially confused the logic with blocking, comparing it to Python's time.sleep() or Selenium's WebDriverWait(). In JavaScript, however, the single-threaded program continues executing other code during the wait, which is why the output shows "Fetching...", "Sync code ran", and finally "Fetched posts:" with the array of posts.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — it may contain errors, so check the original before relying on it.