因为
upload_stream
不返回承诺,如果你想做的是先决的,试试这个:
export async function uploadImage(file: any, location: string, next: any): Promise<string> {
return new Promise((resolve, reject) => {
try {
if (!file) {
reject(new Error("No Image file"));
}
const id = location + "/" + utilities.generatePushID();
const options = {
resource_type: "raw",
public_id: id,
};
return cloudinary.uploader.upload_stream(options, (error: any, result: any) => {
if (error) {
reject(new Error("Couldn't upload"));
}
result(result.public_id);
}).end(file.buffer);
} catch (err) {
reject(InternalError(err));
}
});
}
摆脱
next
因为它看起来像是一个回调,所以您可以这样调用它:
const public_id = await uploadImage(...);
// or
uploadImage(...).then(public_id => console.log(public_id)).catch(() => console.error);
当做。