我想同步运行该功能。在我的应用程序 中,需要在将其分配给其他数据之前创建供应源。 只有完成此任务,应用程序才会进一步执行。 因为否则它将失败,因为创建了其他数据并且找不到SupplySourceId(未发现)。如何在node.js中同步运行此功能
在这里,我要开始同步功能(processSupplySource();)
var articleSupplySourceId = processSupplySource();
功能ProcessSupplySource:
function processSupplySource(){
var postJson2 = {};
postJson2.articleNumber = entry['part-no'];
postJson2.name = entry['part-no'];
postJson2.taxName = 'Vorsteuer';
postJson2.unitName = 'stk';
postJson2.supplierNumber = "1002";
postJson2.articlePrices = [];
var articlePrices = {};
articlePrices.currencyName = 'GBP';
articlePrices.price = entry['ek-preisgbp'];
articlePrices.priceScaleType = 'SCALE_FROM';
articlePrices.priceScaleValue = '1';
postJson2.articlePrices.push(articlePrices);
return postSupplySource(postJson2);
功能PostSupplySource
function postSupplySource(postJson2) {
rp({
method: 'POST',
url: url + '/webapp/api/v1/articleSupplySource',
auth: {
user: '*',
password: pwd
},
body: postJson2,
json: true
}).then(function (parsedBody) {
console.log('FinishArticleSupplySource');
var r1 = JSON.parse(parsedBody);
console.log(r1.id);
return r1.id;
})
.catch(function (err) {
console.log('errArticleSupplySource');
console.log(err.error);
// POST failed...
});
}
阅读回调或承诺 – yBrodsky
你不能。你不应该这样做。你可以很容易地异步运行它,然后按顺序*。 – Bergi