|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我也不知道nodejs的程序问题是不是可以放到这里,如有不对请见谅~
程序是这样的。
- const fs = require('fs')
- var text
- fs.readFile('./test.txt', {encoding:''}, function(err, data){
- if(err){
- console.log(err);
- }else{
- text = data.toString()
- console.log(data.toString());
- }
- });
- console.log('This is the end.')
- console.log('out:',text)
复制代码
输出是这样的
- This is the end.
- out: undefined
- This is a test file.
复制代码
请问 怎么把那个等那个out被赋值之后再输出啊,搞了好久都搞不定。。
本帖最后由 kogawananari 于 2021-9-2 14:20 编辑
- const fs = require("fs")
- const readFile = require("util").promisify(fs.readFile)
- void async function main(){
- let text
- try {
- text = await readFile('./test.txt', "utf-8")
- } catch (err) {
- console.log(`读取失败:${err}`)
- return
- }
- console.log('This is the end.')
- console.log('out:',text)
- }()
复制代码
|
|