我需要在一个循环内执行一个promise,然后将结果解析为另一个promise。但是问题是Promises.all()函数似乎没有接受诺言 代码如下:
return new Promise((resolve, reject) => {
let Path = []
let referencesLoop = []
for (let i = 0; i < Response.length; i++) {
referencesLoop.push(new Promise((resolve, reject) => {
if (assetClass && searchTerm) {
const fileName = `${process.env.FILE_OUTPUTPATH}/${assetClass}/${searchTerm.split(' ').join('_')}/${Response[i]}`
const fileNewName = `${process.env.FILE_OUTPUTPATH}/${assetClass}/${searchTerm.split(' ').join('_')}/${searchTerm.split(' ').join('_')}_${Response[i]}`
const bucketFileName = `${BUCKET_NAME}/${searchTerm.split(' ').join('_')}_${Response[i]}`
if (fs.existsSync(fileName)) {
console.log(Response[i], searchTerm)
fs.rename(fileName, fileNewName,
async function (error, data) {
if (error) {
reject(error)
} else {
try {
const res = await gcs.bucket(BUCKET_NAME).upload(fileNewName, {
// gzip: true,
metadata: {
// Enable long-lived HTTP caching headers
// Use only if the contents of the file will never change
// (If the contents will change, use cacheControl: 'no-cache')
cacheControl: 'public, max-age=31536000',
}
});
const gclouduri = `https://storage.googleapis.com/${bucketFileName}`;
console.log(searchTerm, 'gcloud uri')
console.log(gclouduri)
Path.push(gclouduri);
} catch (error) {
console.error(error)
console.log('error at bucket upload')
Path.push(undefined);
}
}
console.log('^^^^^^^^^^^^^^^^^^^^^^')
console.log(Path)
resolve(Path) //I get the Expected output here
});
}
}
}));
}
console.log(referencesLoop)
Promise.all(referencesLoop).then((ref) => {
console.log('in reference')
resolve(ref)
});
})
我试图查看正在传递到Promise.all中的内容,因此记录了referenceLoop并打印了
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
某个地方的承诺处理似乎是错误的,但我无法指出缺少的地方或确切的内容。