gpt4 book ai didi

Javascript:Promise 实现中出现错误

转载 作者:行者123 更新时间:2023-12-01 00:53:52 25 4
gpt4 key购买 nike

我正在尝试执行异步函数,然后在Promise的帮助下控制台记录结果。恐怕我还没有完全掌握这个概念。

getlinks performs async action.

async function getLinks(param, data) {
return new Promise((resolve, reject) => {
let psub;
var name;
let g;

psub = checkForPsub(param);
var ultUrls = [];

_.each(data, o => {
title = sanitizeString(o.title);
if (psub == true) {
name = title + " u -- " + o.author;
} else {
name = title;
}

switch (o.domain) {
case "i.imgur.com":
{
// downloadImgur(o,name)
}
break;
case "imgur.com":
{
id = o.url.substring(o.url.lastIndexOf("/") + 1);
if (
o.url.includes("https://imgur.com/a/") ||
o.url.includes("https://imgur.com/gallery/") ||
o.url.includes("http://imgur.com/a/") ||
o.url.includes("http://imgur.com/gallery/")
) {
let urls = [];
let file_name;
axios
.get(
"https://api.imgur.com/3/album/" + id,

{ headers: { Authorization: "Client-ID 295ebd07bdc0ae8" } }
)
.then(res => {
let images = res.data.data.images;

_.each(images, function(v) {
var ext = v.link.split(".").pop();
if (ext == "gifv") {
ext = "mp4";
}
if (psub == true) {
file_name =
title + "--" + v.id + " " + "u--" + auth + "." + ext;
} else {
file_name = title + "--" + v.id + "." + ext;
}

let p = { url: v.link, file_name: file_name };
ultUrls.push(p);
});
})
.catch(err => {
console.log(err);
});
}
}
break;
case "i.redd.it":
{
}
break;
default:
console.log("other", o.domain);
}
}); //end each

return resolve(ultUrls);
});
}

我想等到 getlinks 完成执行任务,然后控制台记录结果。

 getLinks(sub,result).then(res =>  console.log({res}))

但即使在 getlink 完成之前,它也会将结果记录为空。

最佳答案

最简单的答案是您 promise 在异步代码之前解析(returnresolve(utlUrls))(axios.get(...) .then(...)) 完成。

这是重现您的问题的最小示例:

let timeout = ms => new Promise(resolve => setTimeout(() => resolve(ms), ms));

async function getLinks(urls) {
return new Promise((resolve, reject) => {
let ultUrls = [];
urls.forEach(url =>
timeout(500).then(res => ultUrls.push(res)))
return resolve(ultUrls);
});
}

getLinks([1, 2, 3]).then(a => console.log(a));

它不起作用,因为我们在填充 ultUrls 之前就返回了它。我们不会等待超时完成。

要解决此问题,只需使用 Promise.all 等待 promise 完成即可。另外删除一些不必要的 promise 包装,我们得到:

let timeout = ms => new Promise(resolve => setTimeout(() => resolve(ms), ms));

function getLinks(urls) {
let ultUrls = [];
let promises = urls.map(url =>
timeout(500).then(res => ultUrls.push(res)))
return Promise.all(promises).then(a => ultUrls);
}

getLinks([1, 2, 3]).then(a => console.log(a));

此外,如果您想使用 async/await 语法,尽管在并行多个请求的情况下它不会给您带来太多好处,但您可以将其编写为:

let timeout = ms => new Promise(resolve => setTimeout(() => resolve(ms), ms));

async function getLinks(urls) {
let ultUrls = [];
let promises = urls.map(url =>
timeout(500).then(res => ultUrls.push(res)))
await Promise.all(promises);
return ultUrls;
}

getLinks([1, 2, 3]).then(a => console.log(a));

关于Javascript:Promise 实现中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56757206/

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