window.ENTITIES={'/api/snippets/typescript/typescript%20-%20simple%20util%20that%20lets%20to%20use%20await%20multiple%20times%20and%20resolve%20them%20together%20by%20single%20call':[{"result":true,"message":null,"batch":{"type":"typescript","name":"typescript - simple util that lets to use await multiple times and resolve them together by single call","items":[{"id":"jPl4lj","type":"typescript","name":"TypeScript - simple util that lets to use await multiple times and resolve them together by single call","content":"type ResolveCallback = (result: T) => void;\ntype RejectCallback = (error?: any) => void;\n\nclass Locker {\n private _promise: Promise | null = null;\n private _resolve: ResolveCallback | null = null;\n private _reject: RejectCallback | null = null;\n private _state: 'result' | 'error' | null = null;\n private _value: T | any | null = null;\n /*\n * Waits if `lock()` method was called until `resolve()` or `reject()` method is called.\n */\n public wait = async (): Promise => {\n if (this._promise) {\n return await this._promise;\n } else {\n switch (this._state) {\n case 'result':\n return this._value as T;\n case 'error':\n throw this._value;\n default:\n throw new Error('Locker has unknown state.');\n }\n }\n };\n /*\n * Locks state by that may be unlocked by `resolve()` or `reject()` method call.\n */\n public lock = (): boolean => {\n if (this._promise) {\n return false;\n }\n const executor = (resolve: ResolveCallback, reject: RejectCallback): void => {\n this._resolve = resolve;\n this._reject = reject;\n };\n this._promise = new Promise(executor);\n return true;\n };\n /*\n * Unlocks the call to the `wait()` method and returns the result from it.\n */\n public resolve = (result: T): void => {\n if (this._promise) {\n this._promise = null;\n this._state = 'result';\n this._value = result;\n return this._resolve!(result);\n } else {\n throw new Error('Locker is not locked.');\n }\n };\n /*\n * Unlocks the call to the `wait()` method and throws the error from it.\n */\n public reject = (error?: any): void => {\n if (this._promise) {\n this._promise = null;\n this._state = 'error';\n this._value = error;\n return this._reject!(error);\n } else {\n throw new Error('Locker is not locked.');\n }\n };\n}\n\n\n\n// Usage example:\n\nconst locker = new Locker();\n\nconst method = async (): Promise => {\n console.log(`Method called!`);\n try {\n const result = await locker.wait();\n console.log(`Method unlocked! Message: ${result}`);\n } catch (error) {\n console.error(`Method unlocked! Error: ${error}`);\n }\n};\n\nlocker.lock(); // <------------------------------------------------- blocks `locker.wait()` call\n\nmethod();\nmethod();\nmethod();\n\nsetTimeout((): void => locker.resolve('Job done!'), 1000); // <----- resolves `locker.wait()` call after 1 second","source":"","author":{"id":"r0ePGo","name":"Mahir-Bright","avatar":"1629142857766__r0ePGo__w40px_h40px.jpg","points":1341,"role":"BASIC"},"creationTime":1707143811000,"updateTime":1707211074000,"removalTime":null}]}}]};