如何使用递归方法进行同步调用?

时间:2019-10-17 16:00:07

标签: javascript recursion promise async-await synchronous

我正在尝试运行一个调用并阻止递归函数继续运行,直到完成另一个函数。

我尝试使用Promise,但我只是不知道将.then()部分中的内容作为递归调用。

    const htmlString = '<table><tr><td>Bah</td><td>Pooh</td></tr></table>';
    const fragment = 
    document.createRange().createContextualFragment(htmlString);

    function walkTheDOM(node, func) {
        func(node);
        node = node.firstChild;
        while (node) {
            walkTheDOM(node, func);
            node = node.nextSibling;
        }
    }

    function asynchronousCallWantingtoMakeSynchronous() {
       return new Promise((resolve, reject) => {
           setTimeout(function() {
       return resolve ( console.log("should come before another recursive call"))
           }, 0)
       });
    }

    walkTheDOM(fragment, function(node) {
        console.log(node)
        asynchronousCallWantingtoMakeSynchronous.then(function(){

        })
    })

这实际上会打印出什么:

    <table>...</table>
    <tr>...</tr>
    <td>Bah</td>
    "Bah"
    <td>Pooh</td>
    "Pooh"
    "should come before Pooh"

我真正想要的是:

    <table>...</table>
    "should come before another recursive call"
    <tr>...</tr>
    "should come before another recursive call"
    <td>Bah</td>
    "should come before another recursive call"
    "Bah"
    "should come before another recursive call"
    <td>Pooh</td>
    "should come before another recursive call"
    "Pooh"
    "should come before another recursive call"

请记住,setTimeout只是一个示例,我只想使异步调用同步。

1 个答案:

答案 0 :(得分:1)

没有办法使异步函数同步。但是使用Promisesasync-await可以使它更像。您可以使用它们来分散通话

const htmlString = '<table><tr><td>Bah</td><td>Pooh</td></tr></table>';
const fragment = document.createRange().createContextualFragment(htmlString);

async function asyncWalkTheDOM(node, func, intersperse) {
    func(node);
    node = node.firstChild;
    while (node) {
        await intersperse(node)
        asyncWalkTheDOM(node, func, intersperse);
        node = node.nextSibling;
    }
}

async function asynchronousCall(node) {
  return new Promise(function (res, rej) { 
    setTimeout(function() {
      console.log("should come before another recursive call")
      res(node)
    }, 0)
  })
}

asyncWalkTheDOM(fragment, function(node) {
    console.log(node.nodeName)
}, asynchronousCall)