utils.js 748 B

123456789101112131415161718192021222324
  1. import { from, of } from 'rxjs';
  2. import runAsync from 'run-async';
  3. /**
  4. * Resolve a question property value if it is passed as a function.
  5. * This method will overwrite the property on the question object with the received value.
  6. * @param {Object} question - Question object
  7. * @param {String} prop - Property to fetch name
  8. * @param {Object} answers - Answers object
  9. * @return {Rx.Observable} - Observable emitting once value is known
  10. */
  11. export const fetchAsyncQuestionProperty = function (question, prop, answers) {
  12. if (typeof question[prop] !== 'function') {
  13. return of(question);
  14. }
  15. return from(
  16. runAsync(question[prop])(answers).then((value) => {
  17. question[prop] = value;
  18. return question;
  19. })
  20. );
  21. };