Je me convertis vite : pour mon cas imbriqué les await
sont effectivement plus pratiques quand je pars de ton exemple.
En plus, pour revenir aux promesses, c’est bien plus rapide puisque je ne perds plus de temps avec les setTimeout()
.
Edit:
Yes mon for asynchrone fonctionne, avec des await
(merci)
let a=[]
let b=[]
let c=[]
(async function () {
for (var ii in mondict) {
a= await apiGlobal.getMonApi(
mondict[ii].monid
);
b= [
...b.map((x) => Object.assign({}, x)),
...a.data,
];
c.push(
Object.assign(
{},
...Object.values(mondict[ii]).map((x, i, a) =>
["monid", "moncode"].indexOf(
Object.keys(mondict[0])[i]
) > -1
? Object.assign(
{},
{
[Object.keys(mondict[0])[i]]: a[i],
a_courant: [
...Object.values(a.data).map(
(x) => x.monautreid
),
],
}
)
: Object.assign({})
)
)
);
} //fin for
})().then(() => {
// enlève les doublons de b et c
// Tri de c
// met à jour ma variable reactive Vue branchée sur un v-model
REACT.c = [...c];
// met à jour ma variable reactive Vue branchée sur un v-model
REACT.b= [...b];
// met à jour ma variable reactive Vue branchée sur un v-show
// d'un DOM table pointant sur le v-model REACT.b
REACT.b_charge = true;
});
}
}
Édit 2:
Quand je vois l’utilisation de cette fonction asynchrone, il doit sans doute être possible de gérer les appels datatable.net. Mais bon comme ça marche avec setTimeOut()
je le modifierai plus tard.
GetMonApi().then((datas_params) => {
setTimeout(function () {
$("#MaTable").DataTable({ bla datas_params })
}, 250);
})
deviendra sans doute
(async function () {
return await GetMonApi()
})().then((aaa) => {
$("#MaTable").DataTable({ bla aaa })
})