如何解决无法在Node.js中执行Promise
我对node.js完全陌生,我要做的就是使用查询器NPM提出一些问题,将这些问题的答案插入模板文字中并从中创建一个readme.md文件。由于某种原因,我的代码无法正常工作。有人可以指出我在这里做错了什么吗?我觉得我在创建Promise时缺少一些参数?
const inquirer = require("inquirer")
const fs = require("fs")
const util = require("util")
const readme = () => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\```
${data.usage}
\```
## License
${license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `
}
// array of questions for user
const questions = [
{
type: "input",name: "title",message: "What is the title of your project",default: "Not Available"
},{
type: "input",name: "username",message: "What is your Github username?",name: "email",message: "What is your email address?",name: "contact",message: "How do you want people to reach you?",name: "description",message: "Please type a short description of your project ( When you are done,hit ESC & type \":wq\" to exit editor )",name: "installation",message: "How do you install this application? ( When you are done,hit ESC type \":wq\" to exit editor) ",name: "usage",message: "Give example of some ways you can use this application ( When you are done,hit ESC & type \":wq\" to exit editor)",{
type: "list",name: "license",message: "What kind of license would you like to use?",choices: ["MIT","Microsoft Public License","Mozilla Public License 2.0","Academic Free License v3.0","Open Software License 3.0","Creative Commons Attribution 4.0"],{
type: "confirm",name: "contribution",message: "Are you open to contribution?",name: "contributionRequirement",message: "If Yes,What are your requirement for giving contribution?",name: "test",message: "Please give instructions for testing of this project ( When you are done,default: "Not Available"
}
]
// function to write README file
function writeToFile(fileName,data) {
fs.writeFile(fileName,data,"utf8",function(err) {
if (err) {
console.log(err);}
})}
// function to initialize program
const init = new Promise(function(resolve,reject) {
resolve(inquirer.prompt(questions))
})
// function call to initialize program
init.then(writeToFile("test.md",readme)
解决方法
当
readme
获得所有输入时, inquirer
应该与结果一起运行。
const inquirer = require ('inquirer');
const fs = require ('fs');
const util = require ('util');
const readme = data => {
return `
# ${data.title}
License covered under ${data.license}
## Table of Contents
1. [Description](#description)
2. [Installation](#installation)
3. [Usage](#usage)
4. [License](#license)
5. [Contributing](#contributing)
6. [Test](#test)
7. [Question](#question)
## Description
${data.description}
## Installation
${data.installation}
## Usage
\`\`\`
${data.usage}
\`\`\`
## License
${data.license}
## Contributing
Accepting contribution: ${data.contribution}
${data.contributionRequirement}
## Tests
${data.test}
## Questions
- Github Repo :
- Email : ${data.email} `;
};
// array of questions for user
const questions = [
{
type: 'input',name: 'title',message: 'What is the title of your project',default: 'Not Available',},{
type: 'input',name: 'username',message: 'What is your Github username?',name: 'email',message: 'What is your email address?',name: 'contact',message: 'How do you want people to reach you?',name: 'description',message: 'Please type a short description of your project ( When you are done,hit ESC & type ":wq" to exit editor )',name: 'installation',message: 'How do you install this application? ( When you are done,hit ESC type ":wq" to exit editor) ',name: 'usage',message: 'Give example of some ways you can use this application ( When you are done,hit ESC & type ":wq" to exit editor)',{
type: 'list',name: 'license',message: 'What kind of license would you like to use?',choices: [
'MIT','Microsoft Public License','Mozilla Public License 2.0','Academic Free License v3.0','Open Software License 3.0','Creative Commons Attribution 4.0',],{
type: 'confirm',name: 'contribution',message: 'Are you open to contribution?',name: 'contributionRequirement',message: 'If Yes,What are your requirement for giving contribution?',name: 'test',message: 'Please give instructions for testing of this project ( When you are done,];
// function to write README file
function writeToFile (fileName,data) {
fs.writeFile (fileName,data,'utf8',function (err) {
if (err) {
console.log (err);
}
});
}
// function call to initialize program
inquirer.prompt(questions).then(answer => {
writeToFile('test.md',readme(answer))
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。