Fixes #4716: correct the ordering of promises writing