正如标题所述,我想知道是否可以在node.js中使用10-24 20:52:34.944 13879-13879/? D/TESTDB: Found Job Job001
10-24 20:52:34.944 13879-13879/? D/TESTDB: Found Job Job002
10-24 20:52:34.944 13879-13879/? D/TESTDB: Found Job Job003
10-24 20:52:34.944 13879-13879/? D/TESTDB: Found Job Job004
(https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original)作为promisify
?我只能这样做:
readline
答案 0 :(得分:2)
这里有另一种方法。它不使用promisify,我认为更简单:
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function question(query) {
return new Promise(resolve => {
readline.question(query, resolve);
})
}
async function main() {
const name = await question('Whats your name? ');
console.log(`hello ${name}!`);
readline.close();
}
main();
答案 1 :(得分:1)
以下是我如何readline.question
承诺的一个示例:
const readline = require('readline');
const { promisify } = require('util');
const readline = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// Prepare readline.question for promisification
readline.question[util.promisify.custom] = (question) => {
return new Promise((resolve) => {
readline.question(question, resolve);
});
};
// Usage example:
const answer = await promisify(readline.question)('What is your name? ');
readline.close();
console.log('Hi %s!', answer);
用于自定义承诺函数的节点(v8)文档:https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_custom_promisified_functions
答案 2 :(得分:0)
尝试使用创建http://bluebirdjs.com/docs/api/promise.promisifyall.html
的蓝鸟但如果代码有效。那么我认为你不需要承诺,因为你已经把它作为诺言归还了。