То, что происходит, - то, что многократные изменения происходят одновременно. Простое решение - установить глобальный флаг для предотвращения метода события во время выполнения текущего действия.
constructor() {
this.notHandling = true;
}
hadlechange = async(type:string) => {
if (this.notHandling) {
this.notHandling = false;
console.log(1111111)
let storage = await getData("G");
console.log(22222222)
await bla1();
await bla2();
console.log(333333)
await storeData('blabla');
console.log(4444444)
this.notHandling = true;
}
};
Редактирование: пример класса помощника
class AsyncQueue {
private queue: Function[];
private processing: boolean;
constructor() {
this.queue = [];
this.processing = false;
}
push = (method:any) => {
this.queue.push(method);
this.process();
}
private process = async () => {
if (!this.processing) {
this.processing = true;
for (let action of this.queue) {
await action();
}
this.processing = false;
}
}
}
export const asyncQueue = new AsyncQueue;
Использование помощника
// import example...
import { asyncQueue } from '~/common/helpers';
// Async test method
const tester = (message: string, seconds: number = 1) => {
return new Promise((resolve) => {
console.log('start ' + message);
setTimeout(() => {
console.log(message);
resolve(message);
}, seconds * 1000);
});
};
// Method calls
asyncQueue.push(() => tester('FIRST'));
setTimeout(() => asyncQueue.push(() => tester('SECOND', 2)), 50);
asyncQueue.push(() => tester('THIRD', 4));