UNPKG

suspenders-js

Version:

Asynchronous programming library utilizing coroutines, functional reactive programming and structured concurrency.

86 lines 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Scope_1 = require("./Scope"); const Util_1 = require("./Util"); describe(`Util tests`, () => { it(`race cancels slower coroutine 1`, (done) => { new Scope_1.Scope({ errorCallback: (error) => { done(error); } }) .launch(function* () { let finallyCalled = false; const result = yield* this.suspend(Util_1.race(this.callAsync(function* () { yield Util_1.wait(0); return 0; }), this.callAsync(function* () { try { yield Util_1.wait(5); done(`this should not run`); this.cancel(); return 1; } finally { finallyCalled = true; } }))); if (result === 0 && finallyCalled) { done(); } else { done(`result: ${result} finallyCalled: ${finallyCalled}`); } this.cancel(); }); }); it(`race cancels slower coroutine 2`, (done) => { new Scope_1.Scope({ errorCallback: (error) => { done(error); } }) .launch(function* () { let finallyCalled = false; const result = yield* this.suspend(Util_1.race(this.callAsync(function* () { try { yield Util_1.wait(5); done(`this should not run`); this.cancel(); return 1; } finally { finallyCalled = true; } }), this.callAsync(function* () { yield Util_1.wait(0); return 0; }))); if (result === 0 && finallyCalled) { done(); } else { done(`result: ${result} finallyCalled: ${finallyCalled}`); } this.cancel(); }); }); it(`suspending on a promise`, (done) => { const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } }); scope.launch(function* () { const x = yield* this.suspend(Util_1.promiseSuspender(new Promise((r) => { r(1); }))); if (x !== 1) { done(`unexpected result`); } done(); this.cancel(); }); }); it(`suspending on a promise error`, (done) => { const scope = new Scope_1.Scope({ errorCallback: (error) => { done(error); } }); scope.launch(function* () { try { yield* this.suspend(Util_1.promiseSuspender(new Promise((_, r) => { r(1); }))); } catch (error) { if (error === 1) { done(); this.cancel(); } } }); }); }); //# sourceMappingURL=Util.spec.js.map