Path: blob/main/components/gitpod-protocol/src/util/queue.ts
2500 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { Deferred } from "./deferred";78/**9* Queues asynchronous operations in a synchronous context10*/11export class Queue {12protected queue: Promise<any> = Promise.resolve();1314enqueue<T>(operation: () => Promise<T>): Promise<T> {15const enqueue = new Deferred<T>();16this.queue = this.queue.then(async () => {17try {18const result = await operation();19enqueue.resolve(result);20} catch (err) {21enqueue.reject(err);22}23});24return enqueue.promise;25}26}272829