Sau đây là mã -
Ví dụ
function multiplication(firstValue, secondValue, callback) {
var res = firstValue * secondValue;
var err = isNaN(res) ? 'Something is wrong in input parameter' :
undefined;
callback(res, err);
}
multiplication(10, 50, function (result, error) {
console.log("The multiplication result="+result);
if (error) {
console.log(error);
}
});
multiplication('Sam', 5, function (result, error) {
console.log("The multiplication result="+result);
if (error) {
console.log(error);
}
}); Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo201.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau -
PS C:\Users\Amit\javascript-code> node demo201.js The multiplication result=500 The multiplication result=NaN Something is wrong in input parameter