返回承诺的功能在解决方面称呼自己

发布于 2025-02-06 16:16:05 字数 634 浏览 2 评论 0 原文

我是JavaScript和node.js的初学者,所以请原谅我是否可以认为这个问题太简单。

我想知道,如果我有一个返回承诺的函数,并且在Resolve()中再次调用相同的函数,以某种递归中的函数,这是否会导致堆栈溢出,以防其无法解决?

您可以想象它如下:

var someVariable = await myFunction(someInput)

async function myFunction(myInputValue) {

    return new Promise(function(resolve, reject) {

        // do some computation
        if (someCondition) {
            resolve(true)
            return
        } else {
            resolve(myFunction(myInputValue))
            return
        }
    })
}

我一直在问这个问题,因为我注意到返回指令已被执行,这(我认为)应该处理该函数的上下文堆栈,并避免遇到诸如堆栈溢出之类的问题。我是否缺少某些东西,然后我冒着问题的危险,还是我是对的,这可以被认为是很安全的做法?

I am quite a beginner with javascript and node.js, so forgive me if the question can be considered as too simple.

I was wondering, if I have a function that returns a Promise, and in its resolve() it calls again the same function in a sort of recursion, can this cause a stack overflow in case it does not get resolved?

You can imagine it as it follows:

var someVariable = await myFunction(someInput)

async function myFunction(myInputValue) {

    return new Promise(function(resolve, reject) {

        // do some computation
        if (someCondition) {
            resolve(true)
            return
        } else {
            resolve(myFunction(myInputValue))
            return
        }
    })
}

I was asking this since I noticed the return instruction gets executed, and this should (in my opinion) deallocate the function's context stack and avoid getting issues like stack overflows. Am I missing something and then I am risking issues or am I right and this can be considered quite safe as practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风夜微凉 2025-02-13 16:16:05

myFunction async 函数,因此您可以将其视为始终返回承诺的函数,并且您可以递归地称其为。

解决承诺构造函数

resolutionFunc value 参数可以是另一个承诺对象,在这种情况下,承诺会动态插入前景链中。

而且,也可以安全地从 async 函数

异步函数的返回值被隐式包裹在 promise.resolve - 如果还不是承诺本身(如示例中)。

... where 被描述为返回:

a 如果值是承诺对象,则可以通过给定值或作为值传递的承诺来解决。

但是:应该没有理由返回 中的新承诺 您的使用 async 在这里: async 函数已经包装了可以返回的任何东西,因此您可以跳过 expectlicit 。 (仅当您将回调风格的调用调整为Promises时,才保留您对承诺构造函数的使用,这可以在 async 和non- async function中发生。

var someVariable = await myFunction(someInput)

async function myFunction(myInputValue) {
  // do some computation on myInputValue that awaits something

  if (someCondition) {
    return true;
  } else {
    return myFunction(someModificationOf(myInputValue));
  }
}

)仍然需要根据 somecontition 以及您对 myInputValue 的任何递归修改确定的递归情况是安全的:如果您不保释递归案例,则可能会遇到堆栈溢出,或者您可能 用完了堆的记忆或永远旋转。 (在中async functions,,但总是将承诺的处理程序带有原本空的堆栈per Promises/A+ 2.2.4和ES6

myFunction is an async function, so you can treat it as a function that always returns a Promise, and you can call it recursively.

It is safe to resolve to a Promise in a Promise constructor:

The resolutionFunc value parameter can be another promise object, in which case the promise gets dynamically inserted into the promise chain.

And it is also safe to return a Promise from an async function:

The return value of an async function is implicitly wrapped in Promise.resolve - if it's not already a promise itself (as in the examples).

...where Promise.resolve is described as returning:

A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object.

However: There should be no reason to return a new Promise in your use of async here: The async function already wraps anything it can return in a Promise, so you can skip the explicit Promise construction antipattern. (Reserve your use of the Promise constructor only when you are adapting a callback-style call into promises, which can happen in async and non-async functions.)

var someVariable = await myFunction(someInput)

async function myFunction(myInputValue) {
  // do some computation on myInputValue that awaits something

  if (someCondition) {
    return true;
  } else {
    return myFunction(someModificationOf(myInputValue));
  }
}

You'll still need to check that this recursive case is safe based on someCondition and whatever recursive modifications you do to myInputValue: If you don't bail out of your recursive case, then you might encounter a stack overflow, or you might run out of heap memory or spin forever. (In async functions, the function runs synchronously up until the first await, but Promise handlers are always called with an otherwise-empty stack per Promises/A+ 2.2.4 and ES6.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文