我有以下使用肥皂网络服务的代码:
var soap = require('soap');
var url = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';
var args = { ubiNum: '1234' };
soap.createClientAsync(url).then((client) => {
return client.NumberToWordsAsync(args);
}).then((result) => {
console.log(result[0].NumberToWordsResult)
});
我正尝试使用await重写它,这是我的尝试:
var soap = require('soap');
var url = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';
var args = { ubiNum: '1234' };
var client = await soap.createClientAsync(url);
var result = await client.NumberToWordsAsync(args);
console.log(result[0].NumberToWordsResult)
但是出现以下错误:
var client = await soap.createClientAsync(url);
^^^^^
SyntaxError: await is only valid in async function
我想念什么?
顺便说一句:如果有人想玩,这里有一个repl:https://repl.it/@opensas/BasicNodeSoapClientWithPromises
答案 0 :(得分:1)
await仅在异步函数中起作用。除了异步函数,您不能放置await语句。因此,您可以重写您的代码,例如
(async function() {
var soap = require('soap');
var url = 'http://www.dataaccess.com/webservicesserver/numberconversion.wso?WSDL';
var args = { ubiNum: '1234' };
var client = await soap.createClientAsync(url);
var result = await client.NumberToWordsAsync(args);
console.log(result[0].NumberToWordsResult);
})();