Loops 仅在方法完成后继续循环

Loops 仅在方法完成后继续循环,loops,promise,lodash,Loops,Promise,Lodash,我希望下面的代码能够同步运行。 每个inquirer.prompt()都需要在另一个之后运行 这是我现在的代码: _.forEach(diff.wrongVersion, (dependency) => { choices = [ 'project version: ' + dependency.projectVersion, 'layer version: ' + dependency.layerVersion]; inquirer.prompt({ ty

我希望下面的代码能够同步运行。 每个
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
    });
});
有人能帮我吗


我认为这可以通过
承诺来实现,但我不知道如何实现。

如果我理解正确,您需要以下原则:

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();
var查询器={};
inquirer.prompt=函数(对象){
返回新承诺(功能(解决、拒绝){
setTimeout(function(){/您可以使用或来实现这种类型的顺序提示

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);
});
/=============模拟查询器模块=====================
var查询器={
提示:功能(问题){
var choices=question.choices
.map((v,i)=>`[${i+1}]-${v}`)
.join('\n');
var message=`${question.message}\n${choices}`;
var result={};
var回答;
返回新承诺((解决、拒绝)=>{
做{
answer=parseInt(窗口提示(消息));
}当(
伊斯南(回答)
答案<1 | |
答案>选择。长度| |
答案===null
);
如果(答案===null){
拒绝();
}否则{
结果[问题.名称]=问题.选择[答案-1];
决心(结果);
}
});
}
};
常数差={
错误版本:[
{
项目版本:“pv-1.0”,
layerVersion:'lv-1.0',
姓名:'Dep-A'
},
{
项目版本:“pv-1.0”,
layerVersion:'lv-1.0',
名称:“Dep-B”
},
{
项目版本:“pv-1.0”,
layerVersion:'lv-1.0',
名称:“Dep-C”
},
{
项目版本:“pv-1.0”,
layerVersion:'lv-1.0',
姓名:'Dep-D'
},
{
项目版本:“pv-1.0”,
layerVersion:'lv-1.0',
姓名:'Dep-E'
},
]
};
diff.ErrorVersion.reduce((承诺,依赖)=>promise.then(结果=>
询问者提示({
键入:“列表”,
name:dependency.name,
消息:`选择要用于的版本:${dependency.name}`,
选择:[
`项目版本:${dependency.projectVersion}`,
`图层版本:${dependency.layerVersion}`
]
})
.then(answer=>Object.assign(result,answer))
),Promise.resolve({}))。然后(结果=>{
控制台日志(结果);
});