调用 promise 上的 resolve() 不会改变其状态,它仍然保持为 pending。

huangapple go评论74阅读模式
英文:

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 &quot;node-fetch&quot;
import { joinCookieNameAndValue } from &quot;../string-manipulation&quot;
import item from &quot;../../types/data-types/item&quot;

const scan = (name: string, page: number): Promise&lt;item | void&gt; =&gt; new Promise(async (resolve, reject) =&gt; {
    const itemsRes: Response = await fetch(`url`, {
        method: &quot;GET&quot;,
        headers: {
            Cookie: joinCookieNameAndValue(&quot;cookie-name&quot;, &lt;string&gt; 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&lt;item&gt; = await itemsRes.json();
    const item_: item | undefined = items.find(i =&gt; i.name == name);

    if (items.length == 0) {
        return resolve();
    }

    if (!item_) {
        return scan(name, ++page);
    }

    resolve(item);
})

export default async (name: string) =&gt; 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&lt;item | null&gt; {
  const itemsRes: Response = await fetch(`url`, {
    method: &quot;GET&quot;,
    headers: {
      Cookie: joinCookieNameAndValue(&quot;cookie-name&quot;, &lt;string&gt; process.env.COOKIE_VALUE)
    }
  })

  if (!itemsRes.ok) {
    throw new Error(`Could not fetch items: request failed with status code ${itemsRes.status}`);
  }

  const items: Array&lt;item&gt; = await itemsRes.json();
  const item_: item | undefined = items.find(i =&gt; i.name == name);

  if (items.length == 0) {
    return null;
  }

  if (!item_) {
    return scan(name, page+1);
  }

  return item;
}

export default (name: string) =&gt; scan(name, 1);

huangapple
  • 本文由 发表于 2023年6月9日 04:55:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435635.html