嗨,编码员我对此代码有疑问:
const ForVideo = async data =>
data.map(story => {
return {
videoUrl: story.video.versions[0].url,
instagramId: story.pk,
videoFilename: MediaDownloader({
url: story.video.versions[0].url,
dest: "/Users/Hernan/Haip/media/" + story.account.pk + "/story/"
}),
expiresAt: story.expiringAt,
tappableObjects: HashMention(story),
influencerId: story.account.pk,
takenAt: story.takenAt,
isVideo: true,
videoDuration: story.video.duration,
displayUrl: story.imageVersions2.candidates[0].url,
imageFilename: MediaDownloader({
url: story.imageVersions2.candidates[0].url,
dest: "/Users/Hernan/Haip/media/" + story.account.pk + "/story/"
}),
callToAction: null
};
});
我调用ForVideo (data)
函数返回一个新的JSON,但问题是这不会返回项目videoFilename
和imageFilename
(来自MediaDownloader()
函数的URL)
我如何应用异步/等待或答应获取完整的JSON并等待MediaDownloader()
函数完成?
MediaDownloader()
:
MediaDownloader: async options => {
let dir = options.dest;
try {
fs.ensureDirSync(dir);
const { filename, image } = await download.image(options);
return filename;
} catch (e) {
console.error(e);
}
}
答案 0 :(得分:0)
尝试一下。
const ForVideo = data =>
data.map(async story => {
return {
videoFilename: await MediaDownloader({
url: story.video.versions[0].url,
dest: "/Users/Hernan/Haip/media/" + story.account.pk + "/story/"
})
};
});
MediaDownloader: options => {
return new Promise((resolve, reject) => {
let dir = options.dest;
try {
fs.ensureDirSync(dir);
const { filename, image } = download.image(options);
resolve(filename);
} catch (e) {
reject(e);
}
});
}