Fix an error where a promise could have been rejected after it has already been resolved