相关疑难解决方法(0)

How to run concurrent promises in JS and then wait for them to finish without Promise.all()?

So I need to do something like:

promiseFunction1().then((result) => {

}).catch((err) => {
    // handle err
});

promiseFunction2().then((result) => {

}).catch((err) => {
    // handle err
});

....

promiseFunctionN().then((result) => {

}).catch((err) => {
    // handle err
});

// WAIT FOR BOTH PROMISES TO FINISH
functionWhenAllPromisesFinished();
Run Code Online (Sandbox Code Playgroud)

I cannot use Promise.all, as I DO NOT CARE IF ONE or ALL OF THEM FAIL. I need to be sure that ALL promises have finished. Also, the callback functions in then() are …

javascript node.js promise

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

尽管使用 Node12.x 在 AWS Lambda 上尝试捕获,但未处理的承诺拒绝

我有一个从 AWS Lambda 内部调用其他 AWS Lambda 的承诺列表:

promiseArray.push(lambda.invoke(params).promise())
Run Code Online (Sandbox Code Playgroud)

在另一个函数中,我迭代这些承诺并尝试解决它们:

for (let i = 0; i < promiseArray.length; i++) {
    try {
        let result = await promiseArray[i];
        console.log("Success!");
    } catch (e) {
        console.log("Failed!");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我面临的问题。通常,调用会抛出一个 TimeoutError,该错误不会被 try-catch 块捕获,并通过抛出“未处理的 Promise Rejection”错误来终止 Lambda 执行。请注意,只有在我们在 Lambda 上从 Node 8.10 升级到 12.x 后,此情况才开始出现。

asynchronous node.js promise aws-lambda

2
推荐指数
1
解决办法
2915
查看次数

反模式聚合错误并传递给promise解决js?

我正在循环中处理一个列表,该循环运行async返回一个promise我不想退出处理异常,所以我聚合它们并将它们传递给外部finally块中的resolve回调.我想知道这是否是反模式,如果是,请提供一个如何正确执行的指针.谢谢.

async doSomething(list) {
let errorCount = 0
let errors = []
return new Promise(async (resolve, reject) => {
  try {
    list.forEach(async (item) => {
      try {
        actionThatThrows(item)
      } catch (e) {
        errorCount++
        errors[errorCount] = e
      }
    })
  } catch (e) {
    errorCount++
    errors[errorCount] = e
  } finally {
    if (errorCount > 0) {
      resolve(errors)
    } else {
      resolve()
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

}

javascript es6-promise

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

如何在JavaScript Promise.all()中使用async / await语法忽略错误

我正在尝试运行一个for循环,该循环将一堆异步请求排队。一旦所有的请求完整,独立的是否resolve还是reject,我希望再运行一些代码。我试图利用异步/等待模式,因为它看起来更好。:)

这就是我在做什么:

var promises = []
for ( item in list ) {
    prom = AsyncFunction( item )
    promises.push(prom)
}

await Promise.all(promises)

doMoreAfter()
Run Code Online (Sandbox Code Playgroud)

但是,某些承诺失败了,而第二个承诺也失败了Promise.all()

我只想忽略所有失败的承诺,并在所有承诺完成后运行下一个代码。

我发现像解决

Promise.all([a(), b(), c()].map(p => p.catch(e => e)))
  .then(results => console.log(results)) // 1,Error: 2,3
  .catch(e => console.log(e));
Run Code Online (Sandbox Code Playgroud)

但是,当尝试将其转换为异步/等待格式时,它似乎并不起作用。

await Promise.all(promises.map(p => p.catch(e => e)))
Run Code Online (Sandbox Code Playgroud)

我想念什么?

javascript promise async-await

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

并行调用一系列 Promise,但按顺序解析它们,无需等待其他 Promise 解析

我有一系列我想并行调用但同步解决的承诺。

我编写了这段代码来完成所需的任务,但是,我需要创建自己的对象QueryablePromise来包装Promise我可以同步检查以查看其已解决状态的本机对象。

有没有更好的方法来完成这个不需要特殊对象的任务?

请注意。我不想使用,Promise.all因为我不想在处理承诺的影响之前等待所有承诺解决。我不能async在我的代码库中使用函数。

const PROMISE = Symbol('PROMISE')
const tap = fn => x => (fn(x), x)

class QueryablePromise {
  resolved = false
  rejected = false
  fulfilled = false
  constructor(fn) {
    this[PROMISE] = new Promise(fn)
      .then(tap(() => {
        this.fulfilled = true
        this.resolved = true
      }))
      .catch(x => {
        this.fulfilled = true
        this.rejected = true
        throw x
      })
  }
  then(fn) {
    this[PROMISE].then(fn)
    return this
  }
  catch(fn) {
    this[PROMISE].catch(fn)
    return this
  }
  static resolve(x) …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous synchronous promise

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

如何将累积的返回Promise值作为数组返回到Array.prototype.reduce()之后的.then()?

鉴于这种模式

someArray.reduce(function(p, item) {
  return p.then(function() {
    return someFunction(item);
  });
}, $.Deferred().resolve()).then(function() {
  // all done here
  // access accumulated fulfilled , rejected `Promise` values
}, function err() {

});
Run Code Online (Sandbox Code Playgroud)

什么方法可以将已完成的,被拒绝的Promise对象的累积值返回到.then(fulfilled)调用后的数组.reduce()

function someFunction(index) {
  console.log("someFunction called, index = " + index);
  var $deferred = $.Deferred();

  window.setTimeout(function() {
    $deferred.resolve();
  }, 2000);

  return $deferred.promise();
}
   
var someArray = [1,2,3,4,5];

someArray.reduce(function(p, item) {
  return p.then(function() {
    return someFunction(item);
  });
}, $.Deferred().resolve()).then(function(data) {
  // all done here
  console.log(data, …
Run Code Online (Sandbox Code Playgroud)

javascript arrays promise jquery-deferred

-5
推荐指数
1
解决办法
2364
查看次数