私は現在競争状態に少し止まっていて、私は自分の髪を引っ張っています。基本的には、APIにクエリを実行し、結果をDBに追加してから、返された/保存されたデータを使って処理します。約束と競争条件
私はこの特定の問題について尋ねることは少なく、この種の問題を解決する方法の設計パターンはますます増えています。行p.push(formatter.createAccounts(account, id));
は最大1000回まで実行され、20秒ほどかかります。
私は
おかげで超参考になります間違ってやって上の任意のアイデア、 Ollie第
// get all users
Promise.all(updatedUsers)
.then(() => {
// create an array of promises
const p = [];
users.then((user) => {
...
ids.forEach((id) => {
const accounts = foo.getAccounts(id, true); // returns a promise
accounts.then((account) => {
// add the return data from here to the promise array at the bottom
p.push(formatter.createAccounts(account, id));
});
});
});
// this would ideally be a promise array of the data from above - but instead it just evaluates to [] (what it was set to).
Promise.all(p).then(() => {
// do my stuff that relies on the data here
})
});
'users'と' ids'はどこから来ますか? –
多分、一貫したサンプルコードを投稿することができますが、現在は構文エラーがあり、変数の半分がどこから来るのかは不明です。 – Tomalak
[関数の内部で変数を変更した後に変数が変更されないのはなぜですか? - 非同期コードリファレンス](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) –