It works, but it's not ideal and can, in the worst case, cause serious problems.It's not.
Two reasons.
1. Revolver cartridges like the 357/38 headspace on the rim. A shorter cartridge is a non-issue since it will still headspace as it should. In an autopistol, a cartridge that is shorter than it should be, headspaces on the extractor. In general, extractors are not made to take a lot of that kind of stress.
2. Unlike revolvers which have a smooth transition in the chamber (since the cartridge headspaces on the rim and not the case mouth) autopistol chambers have a sharp step at the front of the chamber. When shooting the firearm with the proper cartridge, the bullet is forward of this sharp step, but with a shorter cartridge, the bullet can hit that sharp step. When that happens, material can be shaved off and build up at the front of the chamber. In the worst case, that can lead to a partial obstruction which could blow the gun.
If you really can't afford a conversion barrel and really need to shoot .40 in your 10mm autopistol, try to limit how much you shoot through it in any given shooting session and be sure to clean the chamber thoroughly and check your extractor carefully after every shooting sesson. Do not EVER shoot a bunch of .40 through the gun and then switch back to 10mm without cleaning the chamber thoroughly. If there is a buildup, it may prevent the gun from going fully into battery which could lead to an out-of-battery discharge--something to be avoided.
I know a lot of people do it and get away with it, but that doesn't mean it's a good practice or that it is completely without risk.
In general, your owners manual will tell you exactly what ammunition is suitable for your gun. For example, I have a .357Mag revolver and the manual explicitly states that it may also be used with .38Spl or .38Spl +P.
The Glock manual explicitly states that only ammunition "in the caliber of your pistol" should be used and then, for additional emphasis provides a short table which lists only 10mm as being suitable for the Glock 20.