[英]Problem with callback() while using console prompt libraries in Node.js
每次我使用庫時,無論庫是什么,允許我將數據輸入控制台,都會出現不允許我正確使用回調 function 的錯誤。 問題是回調在沒有數據輸入部分的情況下工作,但是當我使用它時,它在 function 之后開始,在主 function 之后開始。
const {prompt} = require('enquirer');
function second(error) {
if (!error) console.log('I\'m third') //but in reality second
}
function main(callback) {
console.log('I\'m first')
const response = prompt({
type: 'input',
name: 'title',
message: 'Please write title'
})
console.log(response); //must be second but in reality third
callback()
}
main(second);
這是控制台 output:
I'm first
Promise { <pending> }
I'm third
✔ Please write title · 1
我認為這是我的錯,因為我是回調和 Node.js 的新手。 如果你能幫助我,我會非常高興。
const {prompt} = require('enquirer');
function second(error) {
if (!error) console.log('I\'m third')
}
async function main(callback) {
console.log('I\'m first')
const response = await prompt({
type: 'input',
name: 'title',
message: 'Please write title'
})
console.log(response);
callback()
}
main(second);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.