let messages = [];
dbOps.getAll(dbCon, "expo_outbox")
.then(function(rows){
// iterate for each message
for (var i = 0; i < rows.length; i++){
mobileMessage = rows[i];
dbOps.getById(dbCon, mobileMessage.mobile_device_id, "mobile_devices")
.then(function(mobileDevice) {
regKey = mobileDevice.reg_key;
console.log(` reg key ${regKey} gets ${mobileMessage.message}`);
messages.push({
to: regKey,
sound: 'default',
body: mobileMessage.message
})
})
}
如何组装此消息数组并让自己能够在知道已完成后台请求的情况下对其进行操作?谢谢...
答案 0 :(得分:1)
您可以使用Promise.all
:
const promises = [
new Promise(resolve => setTimeout(resolve, 0, 1)),
new Promise(resolve => setTimeout(resolve, 0, 2))
];
Promise.all(promises)
.then(data => {
console.log("All promises resolved!", data);
})
.catch(err => {
console.log("Error executing promise");
});
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all