需求: nfc需要与手机交互指令数组,并返回处理。
思路:
1、cardRearder中的promise方法,并返回结果。
2、getRetApdus中for循环处理cardRearder的结果res。(要等待所有的结果返回,使用传统for方法)(forEach相当于一部函数)
3、nfcReadCard请求处理getRetApdus的结果(nfcReadCard请求中的async 要放在res前面 ,否则会报 await is a reserved word)。
结果:console.log("nfcReadCard")会等待nfcReadCard函数执行完。
function cardRearder () {
return new Promise(resolve => setTimeout(resolve, 300));
}
async function getRetApdus(array) {
for (const item of array) {
await cardRearder(item).then(res => {});
}
}
function nfcReadCard(){
API.nfcReadCard({
}).then(async res => {
await getRetApdus();
console.log("nfcReadCard")
})
}
nfcReadCard();