我有下面的Node.js代码。控制回调流程
index.js
"use strict";
// Close dialog with the user, reporting fulfillmentState of Failed or Fulfilled
var service = require("./service.js");
function dispatch() {
const op = service.createIncident("enterpriseID", "shortDesc", function(
incidentNo
) {
if (incidentNo) {
console.log("Index.js ServiceNow Incident:" + incidentNo);
var msg = "Thank you! Your Number is " + incidentNo;
console.log("end of flow");
} else {
console.log("Index.js ServiceNow Incident:" + incidentNo);
msg = "Err";
console.log("end of flow");
}
});
console.log("done");
}
dispatch();
,这里是我的service.js
var request = require("request-promise");
var servicenow = require("./configfile.json");
var snowURL = servicenow.url;
var snowUsername = servicenow.username;
var snowPassword = servicenow.password;
var ticketNo = "00000";
console.log(
"Service Now URL:" +
snowURL +
" Username:" +
snowUsername +
" Password:" +
snowPassword
);
module.exports = {
createIncident: function(caller_id, short_description, callback) {
var snowdetails = {
uri: snowURL,
json: {
short_description: short_description
},
method: "POST",
auth: {
username: snowUsername,
password: snowPassword
}
};
request(snowdetails)
.then(function(body) {
var data = JSON.parse(JSON.stringify(body));
ticketNo = data.result.number;
console.log("Service Now Incident No:" + ticketNo);
callback(ticketNo);
})
.catch(function(err) {
console.log(err);
});
}
};
,当我运行这个程序,我得到的输出
Service Now URL:myUrl
Username:myUserName Password:myPassword
done
Service Now Incident No:INC0010107
Index.js ServiceNow Incident:INC0010107
end of flow
但作为每流量,我需要输出为。
Service Now URL:myUrl
Username:myUserName Password:myPassword
Service Now Incident No:INC0010107
Index.js ServiceNow Incident:INC0010107
end of flow
done
我知道这与回调和承诺有关,但不是如何做的父亲。请大家帮我上我应该改变,以获得输出流量(done
正在打印的最后一件事)
检查出来的'promise'模块。 – ninesalt