Essentially copied from: https://code.google.com/p/selenium/source/browse/javascript/webdriver/promise.js#1252