Reactive Programming with PHP

React Php Basic classes in React PHP

  • Promise
    • Represents result of deferred computation
  • Deferred
    • Represents a pending action which is resolved or rejection
    • Returns single instance of promise

Method handling promises

  • then()
    • Takes two callbacks as arguments
      • The first one is called only when the promise is resolved
      • The second one is called only when it is rejected
    • Each callback can return a modified value which is passed to the next operator
  • done()
    • Takes two callbacks as arguments
      • Returns only null, so it doesn’t allow chaining
      • Intended only to consume the result without modification
  • otherwise()
    • A handler that is called when the promise is rejected
    • Or when the preceding then method throws an exception
  • always()
    • This is the cleanup method called when the method is either resolved or rejected

 

Each method returns a new promise, so we can chain them.

Using promises in PHP

Install React PHP promise library using Composer.

composer require react/promise

 





 

Visit sunny St. George, Utah, USA