Knights were trailing London Scottish 17-16 with just over five minutes left on the clock and facing up the possibility of their sixth home defeat in all competitions this season.
With every passing minute the likelihood of Simon Humberstone’s failed 65th minute conversion attempt costing them their third double of the season increased.
Particularly during a spell of intense Scottish pressure around the 70th minute.
His replacement had hardly had time to break sweat when he was handed the chance to put Knights in front for the first time at 19-17 from 42 metres out.
Foley, whose only previous goals for the club had come in the 27-15 win at Scottish in September, looked the calmest man on the pitch as he hit the target with what proved to be the winning score.
Foley was one of a number of replacements who played their part in rescuing a hard-earned win.
Doncaster’s attack had looked as flat as a pancake at times in a first-half dominated territorially by a spirited and committed Scottish side whose performance throughout belied their second bottom placing.
Only the likes of centre Lesley Klim and No 8 Josh Tyrell on his home debut, troubled the well-drilled Scottish defence in the first half during which Knights only rarely breached the Exiles 22.
After both sides had exchanged penalties Scottish flanker Matt Marley reached out of Tyrell’s tackle after breaking off from a driving maul to score the only try of the first half to give the Exiles a deserved 10-3 lead.
The club’s lowest crowd of the season hoped for better things in the second half when Knights had the wind at their backs.
Their hopes were realised, with second-rower Tom Hicks playing a crucial role in the lineouts, but it continued to be tough going against a never-say-die Scottish side.
Wing Tyson Lewis, who played a crucial role in Knights win, combined with scrum-half Tom James to create the position from which Humberstone pegged three points back with a second penalty on 49 minutes.
But no sooner had the cheers died down than the Exiles increased their lead to 17-6 with a converted try by No 8 Joe Atkinson, who showed surprising pace for a big man to race in from around 20 metres out.
Knights, as did the crowd, knew that they needed to score the next try and did just that when Lewis celebrated signing a new contract in midweek when showing both pace and determination to race in at the corner from over 25 metres.
Lewis was tackled just short after popping up on the opposite flank at pace and making good metres.
Skipper Michael Hills crashed over from close range from the next phase.
Knights: Jarvis, Wilson, Klim, Clark, Lewis, Humberstone,James; List, Hunter, Quigley, Challinor, Hicks, Ram, Hills, Tyrell. Replacements: Nelson, Bergmanas, Sproston, Eames, Hill, Heaney, Foley.