Oracle Magazine, Nov/Dec 2017
Database Developer OPEN SOURCE RESPONDING TO STATE CHANGES To specify what should happen when the state of a promise instance changes promises have two methods then and catch Both methods accept callback functions that may be invoked asynchronously at some point in the future The then method is typically used to specify what should happen when the promise is resolved although it can accept a second function to handle rejections too The catch method is explicitly used to handle rejections The callback functions passed to then and catch will receive the value passed when the resolve or reject function is invoked in the resolver The value passed through the reject function should always be an instance of Error but thats not enforced Heres an example script that uses resolve and reject asynchronously The then and catch methods are used to define what should happen when the promises state changes ORACLE MAGAZINE NOVEMBER OCTOBER 2017 49 function getRandomNumber return new Promise function resolve reject setTimeout function const randomValue Math random const error randomValue 8 true false if error reject new Error Ooops something broke else resolve randomValue 1 2 3 4 5 6 7 8 9 10 11
You must have JavaScript enabled to view digital editions.