Javascript 在节点js module.exports中创建回调
如何在module.exports参数内创建回调函数。 我正在尝试做下面类似的事情,我想知道如何实现回调函数 module.jsJavascript 在节点js module.exports中创建回调,javascript,node.js,Javascript,Node.js,如何在module.exports参数内创建回调函数。 我正在尝试做下面类似的事情,我想知道如何实现回调函数 module.js module.exports = (a, b, callback) => { let sum = a+b let error = null //callback } const add = require(./module.js) add(1,2, (err, result) => { } module.exports = (a
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
app.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
您已使用sum函数;但我将使用divide,因为这样我可以向您展示回调的错误 您的导出将如下所示
module.exports = {
divide: (a,b,cb) => {
if (b === 0) {
cb('divide by zero', null);
} else {
cb(null, a/b);
}
}
}
var func = require('./testExport').divide;
func(1,2,(err,res) => {
console.log(err,res);
});
func(1,0,(err,res) => {
console.log(err,res);
})
而进口是这样的
module.exports = {
divide: (a,b,cb) => {
if (b === 0) {
cb('divide by zero', null);
} else {
cb(null, a/b);
}
}
}
var func = require('./testExport').divide;
func(1,2,(err,res) => {
console.log(err,res);
});
func(1,0,(err,res) => {
console.log(err,res);
})
回调只是从调用函数的地方发送的函数。在两个函数调用(在导入位置)中,您可以看到我们将函数作为包含两个参数的回调发送
在导出的地方,我们调用同一个函数,第一个参数为错误,第二个参数为res
如果要在不使用require().func
的情况下导入函数,则必须在default
中导出函数
module.exports = (a,b,cb) => {
if (b === 0) {
cb('divide by zero', null);
} else {
cb(null, a/b);
}
}
并将其作为
var defaultFunc = require('./testExport')
您已使用sum函数;但我将使用divide,因为这样我可以向您展示回调的错误 您的导出将如下所示
module.exports = {
divide: (a,b,cb) => {
if (b === 0) {
cb('divide by zero', null);
} else {
cb(null, a/b);
}
}
}
var func = require('./testExport').divide;
func(1,2,(err,res) => {
console.log(err,res);
});
func(1,0,(err,res) => {
console.log(err,res);
})
而进口是这样的
module.exports = {
divide: (a,b,cb) => {
if (b === 0) {
cb('divide by zero', null);
} else {
cb(null, a/b);
}
}
}
var func = require('./testExport').divide;
func(1,2,(err,res) => {
console.log(err,res);
});
func(1,0,(err,res) => {
console.log(err,res);
})
回调只是从调用函数的地方发送的函数。在两个函数调用(在导入位置)中,您可以看到我们将函数作为包含两个参数的回调发送
在导出的地方,我们调用同一个函数,第一个参数为错误,第二个参数为res
如果要在不使用require().func
的情况下导入函数,则必须在default
中导出函数
module.exports = (a,b,cb) => {
if (b === 0) {
cb('divide by zero', null);
} else {
cb(null, a/b);
}
}
并将其作为
var defaultFunc = require('./testExport')
add.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
app.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
这里,我们将错误作为第一个参数传递给回调函数,将实际总和作为第二个参数传递给回调函数。假设我们传递一个字符串而不是数字,那么第一个参数将具有错误对象,结果将为null
干杯。add.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
app.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
这里,我们将错误作为第一个参数传递给回调函数,将实际总和作为第二个参数传递给回调函数。假设我们传递一个字符串而不是数字,那么第一个参数将具有错误对象,结果将为null
干杯。在您的模块中。导出您需要“调用”回调函数。 像这样 这将使控件返回到app.js
add()
函数。
您可以在这里实现回调函数。i、 e您希望如何处理收到的结果
下面是您的代码的外观:-
module.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
app.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
在module.exports中,您需要“调用”回调函数。 像这样 这将使控件返回到app.js
add()
函数。
您可以在这里实现回调函数。i、 e您希望如何处理收到的结果
下面是您的代码的外观:-
module.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})
app.js
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
//callback
}
const add = require(./module.js)
add(1,2, (err, result) => {
}
module.exports = (a, b, callback) => {
if (typeof a !== 'number' || typeof b !== 'number') {
return callback(new Error('Invalid argument passed'), null);
}
let sum = a + b;
callback(null, sum);
};
const add = require('./add');
add(1, 2, (err, result) => {
if (err) {
console.log(err);
}
console.log(result);
});
module.exports = (a, b, callback) => {
let sum = a+b
let error = null
callback(error, sum) // invoke the callback function
}
const add = require("./module")
add(1,2, (err, result) => {
if(err) { // Best practice to handle your errors
console.log(err)
} else { // Implement the logic, what you want to do once you recieve the response back
console.log(result)
}
})