2017-08-29 43 views
3

我有下面的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正在打印的最后一件事)

+0

检查出来的'promise'模块。 – ninesalt

回答

0

所以之后服务执行的打印是不是在回调控制台声明。 createIncident已被调用。

这很容易解决,只需移动日志声明回调:

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"); 
     } 
    ); 
} 
0

你应该做的第一件事是恢复被内 createIncident创建的承诺。通过这样做,不需要将回调传递给createIncident,并且您可以直接从返回的承诺中返回ticketNo,并且它将向下传播承诺链。

请注意,通过在此处附加catch处理程序,万一发生错误,链条仍会继续。

createIncident: function (caller_id, short_description/*, callback*/) { 
                //^no need for callback with promises 
    var snowdetails = { 
    // ... 
    }; 

    return request(snowdetails) 
    //^return here 
    .then(function (body) { 
     var data = JSON.parse(JSON.stringify(body)); 
     ticketNo = data.result.number; 
     console.log("Service Now Incident No:" + ticketNo); 
     return ticketNo; // <-- propagate ticketNo through the promise chain 
    }).catch(function (err) { 
     console.log(err); 
    }); 
} 

至于dispatch,你现在可以连锁调用service.createIncident和接收ticketNoincidentNothen回调:在NPM

function dispatch() { 
    return service.createIncident("enterpriseID", "shortDesc") 
    .then(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"); 
     } 
    }); 
} 

dispatch() 
    .then(function() { 
    console.log("done"); 
    })