异步/等待返回值和预言

时间:2018-09-12 11:42:17

标签: javascript node.js asynchronous es6-promise async.js

如果我具有以下异步功能

async fn(){   
    await fn1() // build an updated index from the database

    let res2 = await fn2() // query the database index
        res2 = await fn3(res2) // do something with the index, then update the database

    await fn1() // build an updated index from the database again, but we don't need the new index
    return res2
}

在上述设置中

  • 我希望数据库搜索快速,所以我建立了索引并对其进行了搜索。

  • 建立索引后,我查询数据库(新建立的索引)

  • 然后使用该索引获取一些数据,然后根据结果将一些行添加到数据库中。
  • 此后,我需要上面的查询结果,我可能只是在那里结束代码执行并返回结果,但是我需要使数据库索引保持最新,因此我再次重建索引,但是这次,我真的不需要结果。

如何将上述异步函数结构化为尽可能优化的结构,并减少总体执行时间。

我搜索了一些提供以下代码的示例:

freecodecamp

  const pizzaPromise = selectPizza()
  const drinkPromise = selectDrink()
  await pizzaPromise
  await drinkPromise

我不知道上面的代码有什么区别吗? 不仅是将a-signing的{​​{1}}变成async function,然后是constant value的那个值?

0 个答案:

没有答案