Fixes #4716: correct the ordering of promises writing
Merge pull request #38 from ncharles/bug_4716/dev/error_in_promise_generation

Fixes #4716: correct the ordering of promises writing