我得到了一个Router(Liste_routeur_wifi)对象列表,它使用Flash方法启动新图像的闪存,并在完成后返回一个promise。我通过Wifi连接到路由器,所以我只能一个接一个地做。
我给你一个(工作)示例列表中的2路由器(索引0和1),但我想为N路由器做它,我不知道如何。
如果有人能告诉我这将是非常有帮助的。
wifi.connect({
ssid: Liste_routeur_wifi[0].ssid,
password: Liste_routeur_wifi[0].password
}, async function(err) {
if (err) {
console.log(err);
} else {
//If im connected
console.log('Connexion établie');
//flash method return a promise with the flashed router Liste_routeur_wifi[0].flash(browser).then(function(routeurflash0) {
console.log(routeurflash0);
//when the promise is complete, i wouldlike to go to the next router
wifi.connect({
ssid: Liste_routeur_wifi[1].ssid,
password: Liste_routeur_wifi[1].password
}, async function(err) {
if (err) {
console.log(err);
} else {
console.log('Connexion établie'); Liste_routeur_wifi[1].flash(browser).then(function(routeurflash1) {
console.log(routeurflash1);
})
}
});
递归怎么样?
也许并非100%正确满足您的需求
connectWifi(0, routerAmount);
function connectWifi(index, n){
wifi.connect({
ssid: Liste_routeur_wifi[index].ssid,
password: Liste_routeur_wifi[index].password
}, function(err) {
if (err) {
console.log(err);
} else {
console.log('Connexion établie');
Liste_routeur_wifi[index].flash(browser).then(function(routeurflash) {
console.log(routeurflash);
if(index <= n){
return connectWifi(index++, n);
} else {
return true;
}
})
}
});
}
你有2个选择。
如果您可以并行执行,则可以使用Promise.all
提供你想一个接一个地做:
return Liste_routeur_wifi.reduce(
(current, next) =>
current
.then(t => next.flash())
.catch(err => {console.log(err);}),
Promise.resolve(),
)