【问题标题】:create a promise which encompass a recursive function创建一个包含递归函数的承诺
【发布时间】:2019-05-27 21:32:43
【问题描述】:

我正在编写一个简单的递归函数,它调用一个函数driver,它返回一个承诺。我的 aaa 函数必须在调用结束时返回一个 Promise。

所以这段代码是我的问题的简化:

代码:

function aaa(index) {
      driver(index)
      .then(index => {
            if (index < 100)
                  aaa(index);
            else
                  console.log('finito' + index);     
      })

}

function driver(index) {
      return new Promise(resolve => {
            resolve(index + 1);
      });
}

aaa(0);

我的解决方案:

function aaa(index) {
      console.log(index);
      return Promise.resolve(index)
            .then((index) => {
                  driver(index)
                        .then( index => {
                              if (index < 100)
                                    return aaa(index);
                              else
                                    return Promise.resolve(index);
                        });
            });
}

function driver(index) {
      return new Promise(resolve => {
            resolve(index + 1);
      });
}

function doTheThing() {
  Promise.resolve(0).then(aaa)
  .then(()=>{
    alert('end');
  });
}

doTheThing();

但我在aaa 函数的最后一个then 中仍然有一个编辑器警告,即:

Argument of type '(index: {}) => Promise<void> | Promise<{}>'
is not assignable to parameter of type '(value: {}) => void | PromiseLike<void>'.
  Type 'Promise<void> | Promise<{}>' is not assignable to type 'void | PromiseLike<void>'.
    Type 'Promise<{}>' is not assignable to type 'void | PromiseLike<void>'.

【问题讨论】:

  • 哦哦不!!!谁不喜欢我的问题!!!解释为什么!!!!!!
  • 首先你的问题是什么?乍一看'aaa'是无效函数,所以你不能调用aaa(0)。然后......
  • 我想将其转换为返回承诺的异步函数
  • 我要等到这个函数结束才能执行一些代码!!!我被屏蔽了!
  • 我不确定您对此有何期望,但是...将解析函数作为递归传递,不要再次调用驱动器...

标签: typescript recursion promise es6-promise


【解决方案1】:

我的aaa 函数必须在调用结束时返回一个 Promise

...这正是您的第一个代码中没有发生的事情。但是带有doTheThing 的版本也会出错,因为在driver(index) 的行中没有return

要让它返回一个承诺,你可以坚持使用你的代码的第一个版本,但在两个地方添加return

function aaa(index) {
    return driver(index).then(index => {
//  ^^^^^^ (1)
        if (index < 100) {
            return aaa(index);
//          ^^^^^^ (2)
        } else {
            console.log('finito' + index);     
        }
    })
}

function driver(index) {
    return new Promise(resolve => {
        resolve(index + 1);
    });
}

function doTheThing() {
    Promise.resolve(0).then(aaa).then(() => {
        console.log('end');
    });
}

doTheThing();

请注意,在doTheThing 中,真的 不需要使用Promise.resolve(0).then(aaa).then。你可以做aaa(0).then

【讨论】:

    【解决方案2】:

    你真的不需要做任何特别的事情——

    const aaa = async n =>
      n < 100
        ? driver (n) .then (aaa)
        : n
    
    const driver = async n =>
      n + 1
    
    aaa (0) .then
      ( res => console .log ("res", res)
      , err => console .error ("err", err)
      )
      // res 100

    以上,async 函数保证返回一个 Promise。但如果你不相信它仍然有效,这里有一些补充证明:D

    const aaa = async n =>
    { if (n >= 100)
        return n
      else if (n % 10 === 0)
        return status (n) .then (driver) .then (aaa)
      else
        return driver (n) .then (aaa)
    }
    
    const driver = async n =>
      new Promise (r => setTimeout (r, 15, n + 1)) // simulate 15ms delay
    
    const status = async n =>
    { console .log ("progress: %d%", n)
      return n
    }
    
    aaa (0) .then
      ( res => console .log ("res", res)
      , err => console .error ("err", err)
      )

    输出

    progress: 0%
    progress: 10%
    progress: 20%
    progress: 30%
    progress: 40%
    progress: 50%
    progress: 60%
    progress: 70%
    progress: 80%
    progress: 90%
    res 100
    

    【讨论】:

    • 谢谢兄弟,你是真正的 JavaScript 食尸鬼。
    猜你喜欢
    • 2017-09-21
    • 2014-06-30
    • 2018-12-01
    • 2019-05-25
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    相关资源
    最近更新 更多