G-code wait for extrude command to finish I have following G-code to prime the nozzle before start of the print.
G28 ; home all axes
G0 Z5 F5000 ; lift nozzle
G0 X30; move to X30
G28 Y; home Y
M420 S1; turn on bed leveling
M109 S220; wait for hotend temperature
G1 E20 F1800; extrude filament 20mm

The idea is to extrude a strand of filament outside of the bed and then start the print. The strand catches on the bed a tears off. So the ooze does not mess up the first layer.
The problem is that the G1 E20 F1800 does not wait for the move to finish and the controller goes to next move immediately. This means the nozzle is going to start the first layer, while spewing filament along the way.
Is there a way to wait for the move to finish?
I have tried M400 which seems not to help. I'm using Marlin Firmware.
Are you sure the move didn't finish? That would be very unusual, not the way 3D printer firmware normally operates. A new G0/G1 move command does not execute until the previous one finishes, whether it's extrude-only, travel-only, or a print move (mix of extrusion and travel).
What's probably happening for you is that the amount of material you're trying to extrude cannot melt and pass through the extruder at the speed you requested. At 1800 mm/min, assuming 1.75 mm diameter filament, you're requesting over 72 mm³ of material (nearly a whole cubic centimeter!) of material to be extruded per second. According to some back-of-the-envelope calculations I just did, It would take more than 300 Watts to continuously raise PLA from room temperature to extrusion temperature at that rate, which is not happening without an extreme hotend and power supply.
So, what you're getting is pressure building up between the extruder gear and the hotend (until it starts slipping), causing the material to continue to ooze for some time after the E-axis move finishes, until all the pressure subsides. If your goal is to prime the nozzle for printing, this is not how to do it. It will just end up oozing material all over the start of your print.
You can somewhat fix this by just reducing the feedrate in your command, but that's still not necessarily going to give you great results. The right way to prime is to extrude at nearly the maximum rate your hotend can handle, then retract and wipe before traveling to where the print will begin.