相关疑难解决方法(0)

等到所有ES6承诺完成,甚至拒绝承诺

假设我有一组承诺正在发出网络请求,其中一个会失败:

// http://does-not-exist will throw a TypeError
var arr = [ fetch('index.html'), fetch('http://does-not-exist') ]

Promise.all(arr)
  .then(res => console.log('success', res))
  .catch(err => console.log('error', err)) // This is executed   
Run Code Online (Sandbox Code Playgroud)

让我们说我想等到所有这些都结束了,不管一个人是否失败了.对于我可以没有的资源,可能存在网络错误,但如果我可以获得,我希望在继续之前.我想优雅地处理网络故障.

既然Promise没有留下任何空间,那么在不使用promises库的情况下,处理此问题的推荐模式是什么?

javascript promise es6-promise

351
推荐指数
6
解决办法
11万
查看次数

等待多个并发等待操作

如何更改以下代码,以便触发异步操作并同时运行?

const value1 = await getValue1Async();
const value2 = await getValue2Async();
// use both values
Run Code Online (Sandbox Code Playgroud)

我需要做这样的事情吗?

const p1 = getValue1Async();
const p2 = getValue2Async();
const value1 = await p1;
const value2 = await p2;
// use both values
Run Code Online (Sandbox Code Playgroud)

javascript promise async-await es2017

31
推荐指数
2
解决办法
3820
查看次数

Async/await not working AWS lambda, skipping everything after await

I'm trying to use AWS lambda to test a few API calls using axios, however I'm having some trouble. Every post I came across said the best way to handle promises in Lambda was to use async/await rather than .then, so I made the switch. When I run the program using node it works perfectly, but when I invoke the Lambda locally, it seems like everything after the axios call is being skipped. When I invoke the Lambda …

amazon-web-services node.js async-await aws-lambda

8
推荐指数
1
解决办法
6010
查看次数

串行执行 Promise.all

我有一个包含一系列承诺的数组,每个内部数组可以有 4k、2k 或 500 个承诺。

总共有大约 60k 个 promise,我也可以用其他值来测试它。

现在我需要执行 Promise.all(BigArray[0]).

一旦第一个内部数组完成,我需要执行下一个 Promise.all(BigArray[1]),依此类推。

如果我尝试执行Promise.all(BigArray)它的投掷:

致命错误 call_and_retry_2 分配失败 - 进程内存不足

我需要按顺序执行每个 promise,而不是并行执行,我认为这就是 Node 所做的。我不应该使用新的库,但我愿意考虑答案!。

编辑:

下面是一段示例代码:

function getInfoForEveryInnerArgument(InnerArray) {
    const CPTPromises = _.map(InnerArray, (argument) => getDBInfo(argument));
    return Promise.all(CPTPromises)
        .then((results) => {
            return doSomethingWithResults(results);
        });
}
function mainFunction() {
    BigArray = [[argument1, argument2, argument3, argument4], [argument5, argument6, argument7, argument8], ....];
    //the summ of all arguments is over 60k...
    const promiseArrayCombination = _.map(BigArray, (InnerArray, key) => getInfoForEveryInnerArgument(InnerArray));

    Promise.all(promiseArrayCombination).then((fullResults) => …
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise

4
推荐指数
1
解决办法
7668
查看次数

使用 Promise.all() 获取带有 await 语句的 url 列表

tl; dr - 如果您必须过滤承诺(例如错误的承诺),请不要使用异步函数

我正在尝试使用异步获取 url 列表并解析它们,问题是如果我在获取时其中一个 url 出现错误 - 假设由于某种原因 api 端点不存在 - 程序用明显的错误粉碎解析:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: ext is not iterable
Run Code Online (Sandbox Code Playgroud)

我试过检查 res.json() 是否未定义,但显然不是这样,因为它抱怨整个 'ext' 承诺数组不可迭代。

async function fetchAll() {
  let data
  let ext
  try {
    data = await Promise.all(urls.map(url=>fetch(url)))
  } catch (err) {
    console.log(err)
  }
  try {
    ext = await Promise.all(data.map(res => {
      if (res.json()==! 'undefined') { return res.json()}
    }))
  } catch (err) {
    console.log(err)
  }
  for (let item of ext) {
    console.log(ext) …
Run Code Online (Sandbox Code Playgroud)

javascript

3
推荐指数
1
解决办法
4346
查看次数