• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//// [controlFlowForCatchAndFinally.ts]
2type Page = {close(): Promise<void>; content(): Promise<string>};
3type Browser = {close(): Promise<void>};
4declare function test1(): Promise<Browser>;
5declare function test2(obj: Browser): Promise<Page>;
6async function test(): Promise<string> {
7    let browser: Browser | undefined = undefined;
8    let page: Page | undefined = undefined;
9    try {
10        browser = await test1();
11        page = await test2(browser);
12        return await page.content();;
13    } finally {
14        if (page) {
15            await page.close(); // ok
16        }
17
18        if (browser) {
19            await browser.close(); // ok
20        }
21    }
22}
23
24declare class Aborter { abort(): void };
25class Foo {
26    abortController: Aborter | undefined = undefined;
27
28    async operation() {
29        if (this.abortController !== undefined) {
30            this.abortController.abort();
31            this.abortController = undefined;
32        }
33        try {
34            this.abortController = new Aborter();
35        } catch (error) {
36            if (this.abortController !== undefined) {
37                this.abortController.abort(); // ok
38            }
39        }
40    }
41}
42
43//// [controlFlowForCatchAndFinally.js]
44"use strict";
45var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
46    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
47    return new (P || (P = Promise))(function (resolve, reject) {
48        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
49        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
50        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
51        step((generator = generator.apply(thisArg, _arguments || [])).next());
52    });
53};
54var __generator = (this && this.__generator) || function (thisArg, body) {
55    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
56    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
57    function verb(n) { return function (v) { return step([n, v]); }; }
58    function step(op) {
59        if (f) throw new TypeError("Generator is already executing.");
60        while (_) try {
61            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
62            if (y = 0, t) op = [op[0] & 2, t.value];
63            switch (op[0]) {
64                case 0: case 1: t = op; break;
65                case 4: _.label++; return { value: op[1], done: false };
66                case 5: _.label++; y = op[1]; op = [0]; continue;
67                case 7: op = _.ops.pop(); _.trys.pop(); continue;
68                default:
69                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
70                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
71                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
72                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
73                    if (t[2]) _.ops.pop();
74                    _.trys.pop(); continue;
75            }
76            op = body.call(thisArg, _);
77        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
78        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
79    }
80};
81function test() {
82    return __awaiter(this, void 0, void 0, function () {
83        var browser, page;
84        return __generator(this, function (_a) {
85            switch (_a.label) {
86                case 0:
87                    browser = undefined;
88                    page = undefined;
89                    _a.label = 1;
90                case 1:
91                    _a.trys.push([1, , 5, 10]);
92                    return [4 /*yield*/, test1()];
93                case 2:
94                    browser = _a.sent();
95                    return [4 /*yield*/, test2(browser)];
96                case 3:
97                    page = _a.sent();
98                    return [4 /*yield*/, page.content()];
99                case 4: return [2 /*return*/, _a.sent()];
100                case 5:
101                    if (!page) return [3 /*break*/, 7];
102                    return [4 /*yield*/, page.close()];
103                case 6:
104                    _a.sent(); // ok
105                    _a.label = 7;
106                case 7:
107                    if (!browser) return [3 /*break*/, 9];
108                    return [4 /*yield*/, browser.close()];
109                case 8:
110                    _a.sent(); // ok
111                    _a.label = 9;
112                case 9: return [7 /*endfinally*/];
113                case 10: return [2 /*return*/];
114            }
115        });
116    });
117}
118;
119var Foo = /** @class */ (function () {
120    function Foo() {
121        this.abortController = undefined;
122    }
123    Foo.prototype.operation = function () {
124        return __awaiter(this, void 0, void 0, function () {
125            return __generator(this, function (_a) {
126                if (this.abortController !== undefined) {
127                    this.abortController.abort();
128                    this.abortController = undefined;
129                }
130                try {
131                    this.abortController = new Aborter();
132                }
133                catch (error) {
134                    if (this.abortController !== undefined) {
135                        this.abortController.abort(); // ok
136                    }
137                }
138                return [2 /*return*/];
139            });
140        });
141    };
142    return Foo;
143}());
144