[英]How do I resolve this promise after a for-loop of asynchronous code finishes?
Can't seem to figure out how to make this work without the setTimeout. 似乎无法弄清楚如何在没有setTimeout的情况下进行这项工作。 I want to console log my map only after the asynchronous PostgreSQL stuff is finished and the map contains all the key/value pairs it should.
我只想在异步PostgreSQL内容完成并且该映射包含它应该包含的所有键/值对之后,控制台记录我的映射。
const map = new Map();
pool.query(`SELECT * FROM trips WHERE destination = $1 AND begin_time >= $2 AND begin_time < $3 ORDER BY begin_time`, ['BROWNSVILLE ROCKAWAY AV', '2018-07-18 00:00-04:00', '2018-07-19 00:00-04:00'])
.then(res => {
return new Promise((resolve, reject) => {
const { rows } = res;
resolve(rows);
});
})
.then(res1 => {
return new Promise((resolve, reject) => {
for (let i = 0; i < res1.length; i++) {
if (res1[i + 1]) {
pool.query(`SELECT * FROM get_hwtable($1, $2)`, [res1[i].trip_id, res1[i + 1].trip_id]).then(res => {
const { rows: hwRows } = res;
map.set([res1[i].trip_id, res1[i + 1].trip_id], hwRows);
}).catch(e => console.log('20', e));
}
}
setTimeout(() => {
resolve(map);
}, 8000);
});
})
.catch(e => console.log('25', e))
.finally(function () {
console.log(map);
});
You can simply use Promise.all
on an array of promises returned by pool.query
你可以简单地使用
Promise.all
由归国承诺阵列上pool.query
const map = new Map();
pool.query(`SELECT * FROM trips WHERE destination = $1 AND begin_time >= $2 AND begin_time < $3 ORDER BY begin_time`, ['BROWNSVILLE ROCKAWAY AV', '2018-07-18 00:00-04:00', '2018-07-19 00:00-04:00'])
.then(({rows}) => rows)
.then(res1 => Promise.all(res1.map((r, i) => {
if (res1[i + 1]) {
return pool.query(`SELECT * FROM get_hwtable($1, $2)`, [r.trip_id, res1[i + 1].trip_id])
.then(res => {
const { rows: hwRows } = res;
map.set([res1[i].trip_id, res1[i + 1].trip_id], hwRows);
}).catch(e => console.log('20', e))
}
})))
.catch(e => console.log('25', e))
.finally(function () {
console.log(map);
});
then( res => {..} )
, there is no need to return a Promise
. then( res => {..} )
,不需要返回Promise
。 You can do ``` ```
pool.query().then(res => {
const {rows} = res;
return rows;
}.then ....
``` ```
async
you should use await
and use this for the whole code block. async
,则应该使用await
并将其用于整个代码块。 Could write a promisified timeout function too. ``` ```
///returns a promise that resolves after `duration`
function timeoutPromise(duration) {
return new Promise((resolve, reject) => {
setTimeout(resolve, duration)
})
}
async function doSomething() {
const res = await pool.query()
const {rows} = res;
for (...) {
let hwRows = await pool.query(`SELECT * FROM get_hwtable($1, $2)`, [res1[i].trip_id, res1[i + 1].trip_id]);
map...
}
await timeoutPromise(5000)
}
doSomething()
``` ```
let hwRows = await pool.query()
) will be executed one after another. let hwRows = await pool.query()
)将一次又一次地执行。 use Array.map
to return an array of Promise
and then use Promise.all([Promise])
to get the values. Array.map
返回Promise
数组,然后使用Promise.all([Promise])
获取值。 ``` ```
async function doSomething() {
const res = await pool.query()
const {rows} = res;
let hwRowsPromises = rows.map((row) => {
// ...
// return the promise
return pool.query(`SELECT * FROM get_hwtable($1, $2)`, [res1[i].trip_id, res1[i + 1].trip_id])
})
let hwRowsResults = Promise.all(hwRowsPromises)
await timeoutPromise(5000)
}
``` 4. Promise.all
resolves the values resolved by promises in an array, so you can use convenience like flatten to flatten the array. ```4.
Promise.all
解析数组中由Promise.all
解析的值,因此您可以使用诸如flatten来Promise.all
数组的便利。
``` ```
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]
``` ```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.