等待对象内部的承诺解决

时间:2018-11-07 12:15:32

标签: javascript async-await es6-promise

我有一个对象,其值为promise。对象字段可以具有不同类型的值,例如函数,promise,字符串,数字或其他对象。 如何实现一个等待对象中所有promise解析的函数?

const asyncFunction = () => {
  return Promise.resolve("Resolved!")
}
const nonAsyncFunction = () => {
  return "Resolved!"
}
const awaitObjectWithPromise = (obj) => {
  // Returns a promise which waits for all
  // the promises inside "obj" to be resolved.
}
let obj = {
  key1: asyncFunction(),
  key2: nonAsyncFunction(),
  key3: "Some Value",
  parent1: {
    key1: asyncFunction(),
    key2: nonAsyncFunction(),
    key3: "Some Value"
  }
}

awaitObjectWithPromise(obj).then((obj) => {
  console.log(obj);
  // Should output:
  // {
  //     key1: "Resolved!",
  //     key2: "Resolved!",
  //     key3: "Some Value",
  //     parent1: {
  //         key1: "Resolved!",
  //         key2: "Resolved!",
  //         key3: "Some Value"
  //     }
  // }
})

2 个答案:

答案 0 :(得分:3)

您可以遍历对象的键并兑现承诺。

const asyncFunction = () => {
  return Promise.resolve("Resolved!")
}
const nonAsyncFunction = () => {
  return "Resolved!"
}
const awaitObjectWithPromise = async(obj) => {
  for (let prop in obj) {
    // If the propriety has a 'then' function it's a Promise
    if (typeof obj[prop].then === 'function') {
      obj[prop] = await obj[prop];
    }
    if (typeof obj[prop] === 'object') {
      obj[prop] = await awaitObjectWithPromise(obj[prop]);
    }
  }
  return obj;
}

let obj = {
  key1: asyncFunction(),
  key2: nonAsyncFunction(),
  key3: "Some Value",
  parent1: {
    key1: asyncFunction(),
    key2: nonAsyncFunction(),
    key3: "Some Value"
  }
}

awaitObjectWithPromise(obj).then((obj) => {
  console.log(obj);
});

答案 1 :(得分:2)

在接受的答案中,我对某些事情不满意:

  • 如果其中一个字段是 nullundefined,则会引发错误;
  • 输入值不能是 Promise;
  • 缺乏等待 Promise 的并行性;

为了解决这些问题,我改变了函数:

const promiseAll = async (obj) => {
    if (obj && typeof obj.then == 'function') obj = await obj;
    if (!obj || typeof obj != 'object') return obj;
    const forWaiting = [];
    Object.keys(obj).forEach(k => {
        if (obj[k] && typeof obj[k].then == 'function') forWaiting.push(obj[k].then(res => obj[k] = res));
        if (obj[k] && typeof obj[k] == 'object') forWaiting.push(promiseAll(obj[k]))
    });
    await Promise.all(forWaiting);
    return obj;
}

也许对某人有用。

相关问题