我有 3 个从 Node.js 进行的 Web 服务调用。 2 是 ReST,1 是 SOAP。一切都包含在 Promise 中。
我收到了正确返回 Promise 的 ReST 请求,并且可以在 Promise.all block 中访问这些 Promise,但是当我添加 SOAP 请求时,我收到一条消息,指出 Promise 未定义。
我使用的是 Node v8.2.1。我尝试过请求和请求 promise ,但发生了同样的事情。我的代码看起来像这样 - 我明显做错了什么?
const locationRequest = require('request');
var soapPromise = new Promise(function(resolve, reject) {
locationRequest(options1, function(error, response, output) {
if (error) {
console.info("soap error: " + error);
reject(error);
}
else {
console.info("soap success: " + response);
resolve(response);
}
});
return promise;
});
Promise.all([restPromise, photoPromise, soapPromise]) //addition of soapPromise causes the issue
.then(function([restResult, photoResult, soapResult]) {
//respond to client
console.info("Resource: " + restResult.name);
console.info("Photo Path: " + photoResult);
console.info("Soap: " + soapResult);
})
.catch(function(error) {
console.info("promise all error: " + error);
res.send('done');
//catch an error generated from either request
})
添加 soapPromise
内容给了我:
ReferenceError: promise is not defined
最佳答案
删除 returnpromise;
行。您不需要从 Promise
执行器返回任何内容(您提供 new Promise
的回调),并且它不会创建 promise
变量。因此,此时 promise
是一个未定义的标识符,因此出现 ReferenceError
。
关于javascript - 错误: "ReferenceError: promise is not defined" adding promise to my code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45483238/