【问题标题】:Cleaner Promise.all syntax?更干净的 Promise.all 语法?
【发布时间】:2020-09-02 12:24:08
【问题描述】:

我是 Node.JS 的新手,我真的很讨厌 Promise.all 返回数组的语法。

例如。

const requiredData = await Promise.all([
        getFirst(city),
        getSecond(hubIds),
        getThird(city, customerCategoryKey),
        getFourth(request)
    ])

const firstData = requiredData[0];
const secondData = requiredData[1];
const thirdData = requiredData[2];
const fourthData = requiredData[3];

我需要在单独的代码行中单独获取它们。 有没有办法像

const {
firstData,
secondData,
thirdData,
fourthData
} = await Promise.all([
        getFirst(city),
        getSecond(hubIds),
        getThird(city, customerCategoryKey),
        getFourth(request)
    ])

基本上,如果有比第一个代码 sn-p 更清洁的方法,我真的很想。

TIA!

【问题讨论】:

  • 您可以使用array destructuring 将数组转换为单独的值
  • OP 基本上已经有了。只需使用数组解构而不是对象解构将const { ... } = await 中的括号替换为const [ ... ] = await

标签: javascript node.js promise


【解决方案1】:

如 cmets 中所述,您可以使用 Array destructor 而不是使用对象析构函数:

(async () => {
  const promise1 = Promise.resolve(3);
  const promise2 = 42;
  const promise3 = new Promise((resolve, reject) => {
    setTimeout(resolve, 100, "foo");
  });

  // get all elements as variables
  const [p1, p2, p3] = await Promise.all([promise1, promise2, promise3]);

  console.log(p1, p2, p3);
})();

【讨论】:

  • 此解决方案有效!顺便说一句,我可以直接将这些变量分配给一个对象,所有这些都发生在一行中?
  • 不确定如果您的问题正确,解构仅适用于相同类型,即 Array destruct to Array,Object destruct to Object,所以我认为如果您希望这样做,您必须手动执行此操作在一个对象中。但请检查此answer
【解决方案2】:

如果不是很明显,如果您可以按顺序运行 Promise,您可以 await 它们内联 -

const main = async () =>
{ const a = await mock("one")
  const b = await mock("two")
  const c = await mock("three")
  const d = await mock("four")

  console.log(a, b, c, d)
}

// test funcs
const rand = n =>
  Math.floor(Math.random() * n)

const mock = (x) =>
  new Promise(r => setTimeout(r, rand(1000), x))

// run
console.log("loading...")
main().catch(console.error)

// loading...
// one two three four

如果您想并行运行 Promise,但要按名称检索分配值,我们可以 fromDescriptor 为我们接线 -

const fromDescriptor = (desc = {}) =>
  Promise.all( 
    Object
      .entries(desc)
      .map(([ k, px ]) => px.then(x => [ k, x ]))
  )
  .then(Object.fromEntries)

const main = async () =>
{ const init =            
    { a: mock("one")
    , b: mock("two")
    , c: mock("three")
    , d: mock("four")
    }
  
  const { a, b, c, d } =
    await fromDescriptor(init)

  console.log(a, b, c, d)
}

// test funcs
const rand = n =>
  Math.floor(Math.random() * n)

const mock = (x) =>
  new Promise(r => setTimeout(r, rand(1000), x))

// run
console.log("loading...")
main().catch(console.error)

// loading...
// one two three four

【讨论】:

  • 您对“您可以使用 .then 解构值”的评论表明您只能使用 .then 解构值。我认为仅使用 .then 显示您的示例是一种误导,特别是当作者使用 async/await 时。
猜你喜欢
  • 2016-04-07
  • 2022-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 2019-12-08
  • 1970-01-01
相关资源
最近更新 更多