Fellow Premier League sides Brighton and Hove Albion and Sheffield United also progressed to the fifth round.
Pablo Fornals gave impressive West Ham an early lead at home to Doncaster as it dominated the League One side from the kick-off.
Andriy Yarmolenko doubled the lead before a second-half own goal from Andy Butler and a late strike from debutant Oladapo Afolayan.
West Ham, which is seventh in the Premier League after three straight wins, will play either Manchester United or Liverpool in the next round.
Goals from Yves Bissouma and Steven Alzate were enough to seal a 2-1 win for Brighton against League One Blackpool.
Chris Basham and Billy Sharp scored for Premier League struggler Sheffield United against League One opponent Plymouth Argyle.
Elsewhere, Swansea City thumped fellow EFL Championship side Nottingham Forest 5-1, while Bristol City beat Millwall 3-0. Barnsley upset Championship leader Norwich City 1-0.
On Monday (AEDT), Premier League leader Manchester United faces Liverpool in the tie of the round, while Chelsea hosts Luton Town.