node/tools/eslint/node_modules/inquirer/lib/utils/utils.js
Michaël Zasso 2d441493a4 tools: update eslint to v1.10.3
PR-URL: https://github.com/nodejs/io.js/pull/2286
Reviewed-By: Roman Reiss <me@silverwind.io>
2016-01-13 23:15:39 +01:00

48 lines
1.4 KiB
JavaScript

'use strict';
var _ = require('lodash');
var rx = require('rx-lite');
var runAsync = require('run-async');
/**
* Create an oversable returning the result of a function runned in sync or async mode.
* @param {Function} func Function to run
* @return {rx.Observable} Observable emitting when value is known
*/
exports.createObservableFromAsync = function (func) {
return rx.Observable.defer(function () {
return rx.Observable.create(function (obs) {
runAsync(func, function (value) {
obs.onNext(value);
obs.onCompleted();
});
});
});
};
/**
* Resolve a question property value if it is passed as a function.
* This method will overwrite the property on the question object with the received value.
* @param {Object} question - Question object
* @param {String} prop - Property to fetch name
* @param {Object} answers - Answers object
* @...rest {Mixed} rest - Arguments to pass to `func`
* @return {rx.Obsersable} - Observable emitting once value is known
*/
exports.fetchAsyncQuestionProperty = function (question, prop, answers) {
if (!_.isFunction(question[prop])) {
return rx.Observable.return(question);
}
return exports.createObservableFromAsync(function () {
var done = this.async();
runAsync(question[prop], function (value) {
question[prop] = value;
done(question);
}, answers);
});
};