Rest and diversion
You may want to stay awake for a specific task, so the answer is to get rested at other times, even if you are a student cramming for finals or prepping a thesis.
Alternating activities to keep your mind stimulated may allow you to multitask and complete the goals. Being tired and bored is the recipe for dozing off. Stimulants like caffeine
and some popular drugs are not long term solutions.