我使用async并等待实现此目的,但是以某种方式调用了第二个函数,甚至没有等待第一个函数完成。
异步并等待最佳方法吗?
在这种情况下,我需要在第二个函数内部调用第一个函数还是在另一个init函数内部调用它?
const prompt = require('prompt');
const properties = [
{
name: 'module_name',
default: 'custom-module',
description: "Name your Application module",
type: 'string'
},
{
name: 'application_home',
description: "Provide Application Home location (absolute path)",
message: "Your Application Home location should be a clone of Application Github repo and should have package.json",
type: 'string',
required: true
}
];
let module_name;
let application_home;
/**Prompt user to get mandatory details */
const getModuleDetails = async () => {
prompt.start();
prompt.get(properties, function (err, result) {
if (err) { return onErr(err); }
module_name = result.module_name;
application_home = result.application_home;
return;
});
}
/**Verbose */
const verboseDetails = async () => {
await getModuleDetails()
console.log('Command-line input received:');
console.log('Module Name: ' + module_name);
console.log('Application Home location: ' + application_home);
}
const init = async () => {
//getModuleDetails();
await verboseDetails();
}
init();
更新
/**Prompt user to get mandatory details */
const getModuleDetails = async () => {
prompt.start();
const result = await prompt.get(properties);
module_name = result.module_name;
application_home = result.application_home;
}
const verboseDetails = async () => {
console.log('Command-line input received:');
console.log('Module Name: ' + module_name);
console.log('Botpress Home location: ' + application_home);
}
const init = async () => {
await getModuleDetails();
await verboseDetails();
}
init();
答案 0 :(得分:2)
使用promise,卢克
const prompt = require('prompt');
const properties = [{
name: 'module_name',
default: 'custom-module',
description: "Name your Application module",
type: 'string'
},
{
name: 'application_home',
description: "Provide Application Home location (absolute path)",
message: "Your Application Home location should be a clone of Application Github repo and should have package.json",
type: 'string',
required: true
}];
let module_name;
let application_home;
/**Prompt user to get mandatory details */
const getModuleDetails = async () => new Promise((resolve, reject) => {
prompt.start();
prompt.get(properties, function (err, result) {
if (err) { reject(err); } else {
module_name = result.module_name;
application_home = result.application_home;
resolve(result);
}
});
})
/**Verbose */
const verboseDetails = async () => {
await getModuleDetails()
console.log('Command-line input received:');
console.log('Module Name: ' + module_name);
console.log('Application Home location: ' + application_home);
}
const init = async () => {
//getModuleDetails();
await verboseDetails();
}
init();
答案 1 :(得分:0)
我建议您使用Promise。
Promise是创建Promise时不一定知道的值的代理。它允许您将处理程序与异步操作的最终成功值或失败原因相关联。这样一来,异步方法就可以像同步方法一样返回值:与立即返回最终值不同,异步方法返回了在将来某个时刻提供值的承诺。
以下是MDN文章,介绍了其工作原理以及使用方法:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
答案 2 :(得分:0)
您可以使用util.promisify
来使prompt.get
返回Promise
可以await
以维持所需的执行顺序:
const util = require('util');
let module_name;
let application_home;
const getModuleDetails = async () => {
prompt.start();
const result = await util.promisify(prompt.get)(properties);
module_name = result.module_name;
application_home = result.application_home;
}
答案 3 :(得分:0)
您不是在等待提示结果。
prompt
库也支持async
-使用:
const result = await prompt.get(properties);
代替回调模式。
但是,您将需要在package.json
文件中从git专门请求最新版本。 NPM的最新版本已有5年历史了:
"dependencies": {
"prompt": "git+https://git@github.com/flatiron/prompt.git"
}