gpt4 book ai didi

javascript - nodejs 异步等待流程无法正常工作

转载 作者:行者123 更新时间:2023-12-04 17:15:47 26 4
gpt4 key购买 nike

我想创建一个用于下载视频并将该视频上传到 Youtube 的端点。

所以首先我从请求正文中获取视频 url。其次,我将这段视频保存到临时文件夹中。之后我将这个视频上传到 YouTube channel 最后一步是删除临时视频。

所以我的代码就这样剪掉了:

    const videoUrl = req.body.video_url;
....
....
var download = async function (url: any, dest: any, cb: any) {
var file = fs.createWriteStream(dest);
var request = https.get(url, function (response: any) {
response.pipe(file);
file.on('finish', function () {
file.close(cb);
});
}).on('error', function (err: any) {
fs.unlink(dest);
if (cb) cb(err.message);
});
};

const video = await download(videoUrl, file, function () {
console.log('Video Download Successfull'); ===============> 3rd console output
});
....
....
var readStream = await fs.createReadStream(file);
readStream.on('error', function (err: any) {

});

var request: any = await Youtube.videos.insert({
resource: {
snippet: {
title: title
, description: description
}
, status: {
privacyStatus: "private"
}
}
, part: "snippet,status"
, media: {
body: readStream
}
}, (err: any, data: any) => {
if (err) {
console.log(err.response.data.error)
res.status(500).json({
status: "Fail",
message: "Youtube Video Upload Failed"
})
}
else {
try {
fs.unlinkSync(file)
console.log("temp video file deleted from server"); ===============> 1st console output
} catch (err) {
console.log("temp video file not deleted")
console.error(err)
}
res.status(200).json({
status: "Success",
message: "Youtube Video Upload Successfull"
})
console.log("Video upload to youtube Successfull."); ===============> 2nd console output
}

});

当我调用这个端点时,控制台输出如下:

temp video file deleted from server  
Video upload to youtube Successfull.
Video Download Successfull

但是控制台输出应该是:

Video Download Successfull
Video upload to youtube Successfull.
temp video file deleted from server

为什么我的流程不正确。我该如何解决这个问题?

最佳答案

你将不得不使用 Promises,这里我使用了 promises 下载视频功能。

const videoUrl = req.body.video_url;
....
....
var download = async function (url: any, dest: any, cb: any) {
return new Promise((resolve, reject) => {
try {
var file = fs.createWriteStream(dest);
var request = https.get(url, function (response: any) {
response.pipe(file);
file.on('finish', function () {
file.close(cb);
resolve(true)
});
}).on('error', function (err: any) {
fs.unlink(dest);
if (cb) cb(err.message);
reject(false)
});
}catch (e) {
reject(false)
}
})
};

const video = await download(videoUrl, file)
if (video){
console.log('download success')
}
....
....
var readStream = await fs.createReadStream(file);
readStream.on('error', function (err: any) {

});

var request: any = await Youtube.videos.insert({
resource: {
snippet: {
title: title
, description: description
}
, status: {
privacyStatus: "private"
}
}
, part: "snippet,status"
, media: {
body: readStream
}
}, (err: any, data: any) => {
if (err) {
console.log(err.response.data.error)
res.status(500).json({
status: "Fail",
message: "Youtube Video Upload Failed"
})
}
else {
try {
fs.unlinkSync(file)
console.log("temp video file deleted from server"); ===============> 1st console output
} catch (err) {
console.log("temp video file not deleted")
console.error(err)
}
res.status(200).json({
status: "Success",
message: "Youtube Video Upload Successfull"
})
console.log("Video upload to youtube Successfull."); ===============> 2nd console output
}

});

关于javascript - nodejs 异步等待流程无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68769865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com