【发布时间】:2015-06-18 03:04:20
【问题描述】:
我正在尝试理解以下问题:
var fs = require('fs');
var Promise = require('bluebird');
Promise.promisifyAll(fs);
fs.readFileAsync('read.js','utf8').then(function(content) {
console.log(content);
}).catch(function(error) {
console.log(error);
});
我明白,既然我在承诺 fs 库,我可以使用 readFileAsync 作为承诺。但如果我将readFileAsync 的名称更改为readFileAs,我会收到以下错误:
C:\Users\nniranja\Venkat\promises_github.js:17
fs.readFileA("read.js",'utf8').then(function(content)
^
TypeError: undefined is not a function
at Object.<anonymous> (C:\Users\nniranja\Venkat\promises_github.js:17:4)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
请帮助我理解异步的重要性。谢谢。
【问题讨论】:
-
根本没有名称为
readFileA的方法!我的意思是,你希望fs.niranjan()做什么?完全一样。 -
But if i change the name of readFileAsync to readFileAs- 你是在重新分配还是什么? -
@thefourtheye:我只是将方法名 readFileAsync 改成 readFileAs/readFileA 并再次执行代码。
-
@NiranjanUpadhya 你能展示你尝试过的实际完整代码吗?
-
var fs=require('fs'); var Promise=require('bluebird'); Promise.promisifyAll(fs); fs.readFileA("read.js",'utf8').then(function(content) { console.log(content); }).catch(function(error) { console.log(error); })跨度>
标签: javascript node.js methods bluebird