英文:
Calling resolve() on promise won't change its status which will stay on pending
问题
以下是您要翻译的代码部分:
import fetch, { Response } from "node-fetch"
import { joinCookieNameAndValue } from "../string-manipulation"
import item from "../../types/data-types/item"
const scan = (name: string, page: number): Promise<item | void> => new Promise(async (resolve, reject) => {
const itemsRes: Response = await fetch(`url`, {
method: "GET",
headers: {
Cookie: joinCookieNameAndValue("cookie-name", <string> process.env.COOKIE_VALUE)
}
})
if (!itemsRes.ok) {
return reject(new Error(`Could not fetch items: request failed with status code ${itemsRes.status}`));
}
const items: Array<item> = await itemsRes.json();
const item_: item | undefined = items.find(i => i.name == name);
if (items.length == 0) {
return resolve();
}
if (!item_) {
return scan(name, ++page);
}
resolve(item);
})
export default async (name: string) => scan(name, 1);
希望这能帮助您。
英文:
I have the following code
import fetch, { Response } from "node-fetch"
import { joinCookieNameAndValue } from "../string-manipulation"
import item from "../../types/data-types/item"
const scan = (name: string, page: number): Promise<item | void> => new Promise(async (resolve, reject) => {
const itemsRes: Response = await fetch(`url`, {
method: "GET",
headers: {
Cookie: joinCookieNameAndValue("cookie-name", <string> process.env.COOKIE_VALUE)
}
})
if (!itemsRes.ok) {
return reject(new Error(`Could not fetch items: request failed with status code ${itemsRes.status}`));
}
const items: Array<item> = await itemsRes.json();
const item_: item | undefined = items.find(i => i.name == name);
if (items.length == 0) {
return resolve();
}
if (!item_) {
return scan(name, ++page);
}
resolve(item);
})
export default async (name: string) => scan(name, 1);
The promise above is awaited in another script but the code after it won't ever run as the promise never gets resolved even though the code inside the items.length == 0
if statement runs.
答案1
得分: 2
以下是您要翻译的内容:
"There's a number of strange things going on in your code, but the key issue is indeed that in the second exit of your function, you immediately return and the promise (created with the initial new Promise()) will never resolve.
Also, once a promise has been resolved, it can never be resolved again. Once it transitions from the pending to a resolved/rejected state, that state (and the accompanying value) is permanent.
The best way to solve this though, is to get rid of what's considered a bad thing anyway, don't use new Promise
with async/await.
Here's a rewritten version of the code you shared:
async function scan(name: string, page: number): PromiseLike<item | null> {
const itemsRes: Response = await fetch(`url`, {
method: "GET",
headers: {
Cookie: joinCookieNameAndValue("cookie-name", <string> process.env.COOKIE_VALUE)
}
})
if (!itemsRes.ok) {
throw new Error(`Could not fetch items: request failed with status code ${itemsRes.status}`);
}
const items: Array<item> = await itemsRes.json();
const item_: item | undefined = items.find(i => i.name == name);
if (items.length == 0) {
return null;
}
if (!item_) {
return scan(name, page+1);
}
return item;
}
export default (name: string) => scan(name, 1);
英文:
There's a number of strange things going on in your code, but the key issue is indeed that in the second exit of your function, you immediately return and the promise (created with the initial new Promise()) will never resolve.
Also, once a promise has been resolved, it can never be resolved again. Once it transitions from the pending to a resolved/rejected state, that state (and the accompanying value) is permanent.
The best way to solve this though, is to get rid of what's considered a bad thing anyway, don't use new Promise
with async/await.
Here's a rewritten version of the code you shared:
async function scan(name: string, page: number): PromiseLike<item | null> {
const itemsRes: Response = await fetch(`url`, {
method: "GET",
headers: {
Cookie: joinCookieNameAndValue("cookie-name", <string> process.env.COOKIE_VALUE)
}
})
if (!itemsRes.ok) {
throw new Error(`Could not fetch items: request failed with status code ${itemsRes.status}`);
}
const items: Array<item> = await itemsRes.json();
const item_: item | undefined = items.find(i => i.name == name);
if (items.length == 0) {
return null;
}
if (!item_) {
return scan(name, page+1);
}
return item;
}
export default (name: string) => scan(name, 1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论