utils.js 832 B

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