我希望以下代码运行同步。
每个inquirer.prompt()
都需要在另一个之后运行。
这是我现在的代码:
_.forEach(diff.wrongVersion, (dependency) => {
choices = [ 'project version: ' + dependency.projectVersion, 'layer version: ' + dependency.layerVersion];
inquirer.prompt({
type: 'list',
name: 'dependencies',
message: 'Choose which version to use for ' + dependency.name,
choices
});
});
有人可以帮我吗?
我认为这可以通过Promise
完成,但我不知道如何。
答案 0 :(得分:1)
您可以使用Array#reduce
或lodash#reduce
来实现此类顺序提示。
diff.wrongVersion.reduce((promise, dependency) => promise.then(result =>
inquirer.prompt({
type: 'list',
name: dependency.name,
message: `Choose which version to use for: ${dependency.name}`,
choices: [
`project version: ${dependency.projectVersion}`,
`layer version: ${dependency.layerVersion}`
]
})
.then(answer => Object.assign(result, answer))
), Promise.resolve({})).then(result => {
console.log(result);
});
// =========== Mocking Inquirer Module =====================
var inquirer = {
prompt: function(question) {
var choices = question.choices
.map((v, i) => `[${i+1}] - ${v}`)
.join('\n');
var message = `${question.message}\n${choices}`;
var result = {};
var answer;
return new Promise((resolve, reject) => {
do {
answer = parseInt(window.prompt(message));
} while(
isNaN(answer) ||
answer < 1 ||
answer > choices.length ||
answer === null
);
if(answer === null) {
reject();
} else {
result[question.name] = question.choices[answer-1];
resolve(result);
}
});
}
};
const diff = {
wrongVersion: [
{
projectVersion: 'pv-1.0',
layerVersion: 'lv-1.0',
name: 'Dep-A'
},
{
projectVersion: 'pv-1.0',
layerVersion: 'lv-1.0',
name: 'Dep-B'
},
{
projectVersion: 'pv-1.0',
layerVersion: 'lv-1.0',
name: 'Dep-C'
},
{
projectVersion: 'pv-1.0',
layerVersion: 'lv-1.0',
name: 'Dep-D'
},
{
projectVersion: 'pv-1.0',
layerVersion: 'lv-1.0',
name: 'Dep-E'
},
]
};
diff.wrongVersion.reduce((promise, dependency) => promise.then(result =>
inquirer.prompt({
type: 'list',
name: dependency.name,
message: `Choose which version to use for: ${dependency.name}`,
choices: [
`project version: ${dependency.projectVersion}`,
`layer version: ${dependency.layerVersion}`
]
})
.then(answer => Object.assign(result, answer))
), Promise.resolve({})).then(result => {
console.log(result);
});
答案 1 :(得分:0)
如果我理解正确,您需要遵循以下原则:
var inquirer = {};
inquirer.prompt = function(object) {
return new Promise(function(resolve, reject) {
setTimeout(function() { // <-- here I emulate async execution
console.log('object ==> ', object);
resolve();
}, 1000);
});
}
var diff = {
wrongVersion: [{
projectVersion: 0,
layerVersion: 0,
name: 'zero'
}, {
projectVersion: 1,
layerVersion: 1,
name: 'one'
}, {
projectVersion: 2,
layerVersion: 2,
name: 'two'
}, {
projectVersion: 3,
layerVersion: 3,
name: 'three'
}]
}
var iterator = 0;
function callPrompt() {
var dependency = diff.wrongVersion[iterator];
var choices = ['project version: ' + dependency.projectVersion, 'layer version: ' + dependency.layerVersion];
inquirer.prompt({
type: 'list',
name: 'dependencies',
message: 'Choose which version to use for ' + dependency.name,
choices: choices
}).then(function() {
iterator++;
if (diff.wrongVersion[iterator]) {
callPrompt();
}
});
};
callPrompt();
关于jsfiddle的例子(注意concole) - https://jsfiddle.net/regwtew1/2/