Menu

getCurrentPosition() Has No Default Timeout. That's the Trap.
📰

getCurrentPosition() Has No Default Timeout. That's the Trap.

DEV Community·Parsa J·12 days ago
#cCETVQKW
#dev#getcurrentposition#timeout#every#article
Reading 0:00
15s threshold

The ticket read: "location button sometimes just spins forever." Not a crash, not an error toast — just a spinner that never stopped. QA couldn't reproduce it. It worked every time on the office Wi-Fi, every time on a laptop, every time in the demo to the client.

It only happened out in the field. On a phone. Somewhere the GPS fix took its time and the Wi-Fi-based positioning fallback was slow to answer. Which meant it was exactly the one environment nobody tested in before shipping.

Here's the part that made it worse: the code had an error handler. Both arguments to getCurrentPosition() were filled in, success and failure. By every code-review instinct, this was handled.

The code that looks fully handled

function useMyLocation ( onFound , onFail ) { navigator . geolocation . getCurrentPosition ( ( pos ) => onFound ( pos . coords ), ( err ) => onFail ( err . message ) ); }

Enter fullscreen mode

Exit fullscreen mode

A success callback, an error callback, nothing swallowed.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.