处理来自回调函数的响应

时间:2016-12-14 04:39:31

标签: javascript node.js

我在if function内拨打callback nodefunction会启动并完成其任务,但我无法弄清楚如何从function中检索response

callback

foo() { merchant.createCustomerProfile(cardNumber, expDate, email, id, function callback(){ // How do I retrieve the response from the callback? // console.log(callback); }); //so I can pass it out here and use it } function实际为callback returns。它看起来像这样,

callback

我正在尝试获取应该在回调函数响应中的function createCustomerProfile(cardNumber, expDate, email, id, callback) { ctrl.execute(function(){ var apiResponse = ctrl.getResponse(); var response = new ApiContracts.CreateCustomerProfileResponse(apiResponse); if(response != null) { if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK) { console.log('Successfully created a customer profile with id: ' + response.getCustomerProfileId()); } else { console.log('Result Code: ' + response.getMessages().getResultCode()); console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode()); console.log('Error message: ' + response.getMessages().getMessage()[0].getText()); } } else { console.log('Null response received'); } callback(response); }); }

1 个答案:

答案 0 :(得分:1)

你可以尝试这个:

foo() {
  merchant.createCustomerProfile(cardNumber, expDate, email, id, function callback(response){
              // How do I retrieve the response from the callback?                   
              // console.log(callback);
              console.log(response);
            });
     //so I can pass it out here and use it
}