代码之家  ›  专栏  ›  技术社区  ›  Karim Garali

创建一个包含递归函数的承诺

  •  -1
  • Karim Garali  · 技术社区  · 6 年前

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

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

    代码:

    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();
    

    但我最后还有一个编辑警告 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>'.
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   trincot Jakube    6 年前

    我的 aaa 函数必须在调用结束时返回承诺

    …这正是您的第一个代码中没有发生的事情。还有那个版本 doTheThing 遇到错误,因为没有 return 与…一致 driver(index) .

    为了让它返回一个承诺,您可以坚持使用代码的第一个版本,但是添加 返回 在两个地方:

    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();

    请注意 多事 它不是 真的? 必须做 Promise.resolve(0).then(aaa).then . 你可以做 aaa(0).then .

        2
  •  1
  •   Mulan    6 年前

    你不必做任何特别的事情

    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 职能部门保证会兑现承诺。但如果你不相信它仍然有效,这里有一些额外的证据: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