utils.js 767 B

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