我是异步编程的新手,之前从未做过承诺队列,所以我不知道如何解决这个问题.
我有一张有银行账户的桌子
对于每个银行帐户,我都有一个收据清单
Account 111
- Receipt 001
- Receipt 002
- Receipt 003
Account 222
- Receipt 004
- Receipt 005
- Receipt 006
Run Code Online (Sandbox Code Playgroud)
所以我建立了一个承诺,找到()所有的银行账户.
然后我循环通过所有银行帐户,并为每个帐户我找到所有收据.
我该怎么办?为每个收据find()创建一个承诺?
创建一系列承诺?(你怎么做的顺便说一句)
或者有第三种选择吗?
//
// Find all bank accounts
//
var findBank = new Promise(
(resolve, reject) => {
bankTable.find({}
,function(err, data) {
if (!err) {
resolve(data);
} else {
reject(new Error('findBank ERROR : ' + err));
}
});
});
//
// Find the RECEIPTS for each bank account
//
var findAllReceipts = …Run Code Online (Sandbox Code Playgroud) 为什么异步将返回值转换为“ [object Promise]”
这是我想要工作的代码......
function foo() {
let res = bar("HELLO")
console.log(res)
}
async function bar (text) {
text = text + await getData();
return (text)
}
function getData () {
return new Promise((resolve, reject) => {
// Do a lot of stuff to find myResult
resolve(myResult)
})
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是……这怎么回来 HELLO
function foo() {
let res = bar("HELLO")
console.log(res)
}
function bar (text) {
return (text)
}
Run Code Online (Sandbox Code Playgroud)
这返回 [object Promise]
function foo() {
let res = bar("HELLO")
console.log(res)
} …Run Code Online (Sandbox Code Playgroud) 我对 SSL / https 很陌生,但最终设法为我的网站创建了一个
现在的问题是这个奇怪的错误:
app.7e27c5b6c47cfaa5a0da.js:70 error occured for getting the country from: http://freegeoip.net/json/ {data: null, status: -1, config: {…}, statusText: "", headers: ƒ}
app.7e27c5b6c47cfaa5a0da.js:70 Mixed Content: The page at 'https://thaihome.co.uk/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://freegeoip.net/json/'. This request has been blocked; the content must be served over HTTPS.
Run Code Online (Sandbox Code Playgroud)
并且它以如此快的速度吐出错误以致浏览器几乎死了。
这是什么意思?我能做些什么来修复它?
我在这里尝试了所有操作-将Numbers乘以数组后为什么我的totalArr总数变成NaN?
let amountArr = [];
for (let i=0; i<3; i++) {
// Find Data
let lineAmount = 0;
let selected = printArr.filter(function(obj) {
return obj.property == propertyArr[0] && obj.year == fromYear && obj.month == i+1;
})
if (selected.length) {
console.log("typeof (selected[0].amount): " + typeof(selected[0].amount))
console.log("selected[0].amount: " + selected[0].amount)
lineAmount = Number(selected[0].amount);
console.log("typeof (lineAmount): " + typeof(lineAmount))
console.log("lineAmount: " + lineAmount)
}
amountArr[i] += Number(lineAmount)
console.log("typeof (amountArr[i]): " + typeof(amountArr[i]))
console.log("amountArr[i] :" + amountArr[i])
console.log("-------------------------------------------")
}
console.log(amountArr)
Run Code Online (Sandbox Code Playgroud)
这是console.log的结果
任何帮助都会很大,我真的没有得到这个帮助吗?