因为您正在调用函数,然后立即行退出进程。
process.exit()
在你的
removeHistoryFromOnlineDatabase
函数执行。
请注意:-
我已经删除了较小代码的JSON数据选项。
方式1:
(使用
Callback
)
(仅回拨)
saveHistory(data, function (result) {
console.log("successfully save into local db");
removeDataFromOnlineDatabase(function(error, response){
process.exit();
})
});
function removeHistoryFromOnlineDatabase(callback){
var request = require("request");
console.log("function get called");
var options = options;
request(options, function (error, response, body) {
if (error){
callback(error, null);
} else {
console.log("history has been removed" + body);
callback(null, response)
}
});
}
方式2:
(使用
Promise
)
(仅承诺)
var Q = require("q");
saveHistory(data, function (result) {
console.log("successfully save into local db");
removeDataFromOnlineDatabase()
.then(function(){
process.exit();
}).catch(function(){
console.log("ERROR IN REQUEST");
});
});
function removeHistoryFromOnlineDatabase(){
var request = require("request");
console.log("function get called");
var options = options;
return Q.promise(function(resolve, reject){
request(options, function (error, response, body) {
if (error){
reject(error);
} else {
console.log("history has been removed" + body);
resolve(response)
}
});
});
}
方式3:
(使用
promise - Q.nfcall
)
(回拨+承诺)
var Q = require("q");
saveHistory(data, function (result) {
console.log("successfully save into local db");
Q.nfcall(removeDataFromOnlineDatabase)
.then(function(){
process.exit();
}).catch(function(){
console.log("ERROR IN REQUEST");
});
});
function removeHistoryFromOnlineDatabase(callback){
var request = require("request");
console.log("function get called");
var options = options;
request(options, function (error, response, body) {
if (error){
callback(error, null);
} else {
console.log("history has been removed" + body);
callback(null, response)
}
});
}