使用导出模块将多个parameter passing给NodeJScallback
情况1:
我在我的atom项目weather.js
和exmaple.js
有两个文件,天气是使用导出模块导出一切到example.js和依次example.js使用require模块
我的weather.js
var request = require('request'); module.exports= function(justAnothercallback) { justAnothercallback('This is from weather'); }
myExample.js
var fromWeather = require('./weather.js'); fromWeather(function(weather){ console.log(weather); });
如果我做Node myExample.js
输出是:这是来自天气
情景2:现在我只是在我的weather.js
再传一个callback
module.exports= function(justAnothercallback, SecondCallback) { justAnothercallback('This is from weather'); SecondCallback('This is second callback)'); }
而我的example.js被修改,以适应第二个callback函数!
var fromWeather = require('./weather.js'); fromWeather(function(weather, anotherfunc){ console.log(weather); console.log(anotherfunc); });
从terminal我们得到:
/>节点example-callback.js 这是天气未定义 /Users/NodeCourse/async/weather.js:7 SecondCallback('This is second callback)'); ^
TypeError:SecondCallback不是module.exports中的函数(/ Users / oogway / NodeCourse / async / weath
我的问题是他们不一样 ,我只是加了一个callback,它barfed! 为什么!! 但它工作正常,如果我只传递一个callback..请帮助一个这个。
在你的代码中,你只传递一个带有两个参数的callback函数
var fromWeather = require('./weather.js'); fromWeather(function(weather, anotherfunc){ console.log(weather); console.log(anotherfunc); });
这是两个callback看起来像
var fromWeather = require('./weather.js'); fromWeather(function(){ console.log('Hello from callback 1'); }, function(){ console.log('Hello from callback 2'); });