How to break or come out of a function if no internet connection after 20 sec

Yes. The code.

Since I can't see your code, I am only guessing, but why not simply test the status in the loop?

if ((!narrowband.begin(pinnumber) == NB_READY))