utils.js 813 B

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