async-coalesce
coalesce
async coalesceresolvers: any, errorIfUndefined?: string: Promise<any>;async coalesceresolvers: any, params: any, errorIfUndefined?: string: Promise<any>;
This function is intended for resolving a value potentially defined in higher scopes. Typical use case would be for methods accepting a configuration object whose properties could also be defined in the class constructor or some other greater scope like below:
;
However, this function also handles resolving async functions which traditionally would be done like below:
;;try catch err if typeof result2 === 'undefined' || result2 === null throw 'result2 is not defined';
This approach works, but can get quite verbose. This same line using async-coalesce
is shown below
;;
We are not wrapping this in a try block because the coalesce function will throw the 'Failed to coalesce' exception with the reason if any of the functions error. Some use cases may still require catching the error.
If the second parameter is an array of values, they will be applied as parameters to each function that is a resolver. If each function will accept different parameters, just set the resolver value as the result of the function, and do not include the params array. If the second parameter is a string, this will be the error text.
;
Note
This function will still error if it is passed a parameter that is a property of an undefined value. For example:
; ;
This will throw a "Cannot access '.prop' on undefined
" error. It is recommended that referenced objects be instantiated with a default value.
;
coalesceSync
coalesceSyncresolvers: any, params: any = , errorIfUndefined?: string any
This function is also available for use in a constructor, or some other area were async/await does not work. It does not support async resolvers.