3 ms·
Promises have two awesome things: * With callbacks interface of the function hardcodes whether the function is strictly synchronous or may use any async code.
by pornel 13y ago
Promises have two awesome things:
* With callbacks interface of the function hardcodes whether the function is strictly synchronous or may use any async code. Change of one little helper function from sync to async may have ripple effect throughout the entire codebase (you have to add callback to all its callers, and all their callers, and their callers...).
Promises fix that. Sync and async functions take arguments and return values the same way, so when you change sync to async you only need to change source and consumer of the value, and nothing in between (e.g. same code can cache results of (memoize) promise-returning functions and sync functions).
* With ES6 yield async calls look and behave almost like sync calls. No callbacks. You don't have to use weird error handling schemes — just use try/catch! You don't have to have a library to iterate over things — just use for()!
A call that used to look like:
use(compute(arg))
was twisted by async to be:
compute(arg, use)
Promises change it to:
compute(arg).then(use)
and yield changes it back to nearly original form:
use(yield compute(arg))