Javascript 无法使用异步解决承诺问题
我正在使用igmurapi上传图像。我在Node.js中使用承诺async await编写了一个应用程序。(请注意,我对这些概念相当陌生) 我的代码似乎确实有效。它将图像上传到igmur。但是,问题是,承诺没有得到解决。请参考下面的代码Javascript 无法使用异步解决承诺问题,javascript,node.js,async-await,es6-promise,Javascript,Node.js,Async Await,Es6 Promise,我正在使用igmurapi上传图像。我在Node.js中使用承诺async await编写了一个应用程序。(请注意,我对这些概念相当陌生) 我的代码似乎确实有效。它将图像上传到igmur。但是,问题是,承诺没有得到解决。请参考下面的代码 router.post('/images/upload', async (req, res) => { /* we would receive a request of file paths as array */ let filePath
router.post('/images/upload', async (req, res) => {
/* we would receive a request of file paths as array */
let filePaths = req.body.urls;
let multipleUpload = new Promise(async (resolve, reject) => {
let upload_len = filePaths.length,
upload_res = new Array();
for (let i = 0; i <= upload_len + 1; i++) {
let filePath = filePaths[i];
await imgur.upload(filePath, (error, result) => {
console.log(" A -> ");
if (upload_res.length === upload_len) {
console.log("******");
/* resolve promise after upload is complete */
resolve(upload_res)
} else if (result) {
/*push public_ids in an array */
console.log("OK")
upload_res.push(result.public_id);
} else if (error) {
console.log(error)
reject(error)
}
})
}
})
.then((result) => console.log(result))
.catch((error) => error)
let upload = await multipleUpload;
res.json({
'response': upload
})
})
router.post('/images/upload',异步(req,res)=>{
/*我们将收到一个文件路径作为数组的请求*/
让filepath=req.body.url;
让multipleUpload=新承诺(异步(解析、拒绝)=>{
让upload_len=filepath.length,
upload_res=新数组();
for(设i=0;i{
console.log(“A->”);
如果(上传分辨率长度===上传长度){
console.log(“*******”);
/*上传完成后解决承诺*/
解析(上传_res)
}否则,如果(结果){
/*在数组中推送公共\u ID*/
console.log(“确定”)
上传资源推送(结果公共id);
}else if(错误){
console.log(错误)
拒绝(错误)
}
})
}
})
.然后((结果)=>console.log(结果))
.catch((错误)=>错误)
让上传=等待多重上传;
res.json({
“响应”:上载
})
})
我遵循了这个,它做了类似的事情 在按下
结果后,修复逻辑以检查长度。您从未调用过resolve
例如,如果您收到1个文件,您将推入数组并退出循环
if (result) {
/*push public_ids in an array */
console.log("OK")
upload_res.push(result.public_id);
if (upload_res.length === upload_len) {
console.log("******");
/* resolve promise after upload is complete */
resolve(upload_res)
}
}
在另一个主题中,您应该在出现错误后退出循环,否则您可能会调用reject,然后resolve,这不好。在推送结果后,修复逻辑以检查长度。您从未调用过resolve
例如,如果您收到1个文件,您将推入数组并退出循环
if (result) {
/*push public_ids in an array */
console.log("OK")
upload_res.push(result.public_id);
if (upload_res.length === upload_len) {
console.log("******");
/* resolve promise after upload is complete */
resolve(upload_res)
}
}
在另一个主题中,您应该在出现错误后退出循环,否则您可能会调用reject,然后resolve,这并不好。一个(非常)干净、性能更高的代码版本是:
function uploadToImgur(filePath) {
return new Promise((resolve, reject) => {
imgur.upload(filePath, (err, res) => {
if (err) reject(err);
resolve(res.public_id);
})
})
}
router.post('/images/upload', async (req, res) => {
/* we would receive a request of file paths as array */
let filePaths = req.body.urls;
let promises = filePaths.map(uploadToImgur);
let upload = await Promise.all(promises);
res.json({
'response': upload
})
})
这用于创建一个承诺数组,每个文件路径对应一个承诺,然后用于同时等待所有承诺。一个(非常)干净且性能更高的代码版本是:
function uploadToImgur(filePath) {
return new Promise((resolve, reject) => {
imgur.upload(filePath, (err, res) => {
if (err) reject(err);
resolve(res.public_id);
})
})
}
router.post('/images/upload', async (req, res) => {
/* we would receive a request of file paths as array */
let filePaths = req.body.urls;
let promises = filePaths.map(uploadToImgur);
let upload = await Promise.all(promises);
res.json({
'response': upload
})
})
这用于创建一个承诺数组,每个文件路径对应一个承诺,然后用于同时等待所有承诺。我发现至少有两个问题:
- 当不需要时,可以将async与promise混合使用
- 我怀疑
imgur.upload
不会返回承诺,因为它接受回调函数
我认为这应该解决它:
router.post('/images/upload', async (req, res) => {
let filePaths = req.body.urls
let upload_len = filePaths.length
let multipleUpload = filePaths.map(filePath => {
return new Promise((resolve, reject) => {
imgur.upload(filePath, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result.public_id)
}
})
})
})
let upload = await Promise.all(multipleUpload)
res.json({
response: upload,
})
})
我至少看到两个问题:
- 当不需要时,可以将async与promise混合使用
- 我怀疑
imgur.upload
不会返回承诺,因为它接受回调函数
我认为这应该解决它:
router.post('/images/upload', async (req, res) => {
let filePaths = req.body.urls
let upload_len = filePaths.length
let multipleUpload = filePaths.map(filePath => {
return new Promise((resolve, reject) => {
imgur.upload(filePath, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result.public_id)
}
})
})
})
let upload = await Promise.all(multipleUpload)
res.json({
response: upload,
})
})
上传imgur.upload
是否返回承诺?您指的是哪一个承诺?在旁注中,您应该始终将express的异步处理程序放在try…catch
块中,否则您可能会得到未处理的承诺拒绝。切勿将异步函数
作为新函数的执行器回调传递承诺
构造函数imgur.upload
接受回调,似乎不会返回承诺,无论如何,你不能等待它。@AyushGupta确实等待它。imgur.upload
返回一个承诺?你指的是哪一个承诺?在旁注中,你应该始终将你的express的异步处理程序放在try…catch
块中,否则你会得到一个未处理的承诺拒绝
。永远不要传递异步函数
作为新承诺
构造函数的执行者回调imgur.upload
接受回调,似乎不会返回承诺,无论如何,您不能等待它。@AyushGupta非常感谢您。代码起作用。正如你提到的,我自己的函数并没有返回一个承诺:)非常感谢。代码起作用。正如您提到的,我自己的函数不会返回承诺:)