Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/util/queue.ts
2500 views
1
/**
2
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { Deferred } from "./deferred";
8
9
/**
10
* Queues asynchronous operations in a synchronous context
11
*/
12
export class Queue {
13
protected queue: Promise<any> = Promise.resolve();
14
15
enqueue<T>(operation: () => Promise<T>): Promise<T> {
16
const enqueue = new Deferred<T>();
17
this.queue = this.queue.then(async () => {
18
try {
19
const result = await operation();
20
enqueue.resolve(result);
21
} catch (err) {
22
enqueue.reject(err);
23
}
24
});
25
return enqueue.promise;
26
}
27
}
28
29