-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass Promise as a parameter to regeneratorRuntime.async() (#383)
This makes it possible to run @babel/plugin-transform-runtime on regenerator's output to provide a Promise polyfill in old browsers.
- Loading branch information
1 parent
6e9e8d7
commit 491714f
Showing
4 changed files
with
178 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
var assert = require("assert"); | ||
|
||
function SyncPromise(run) { | ||
this.resolved = null; | ||
this.value = null; | ||
try { | ||
run( | ||
val => { this.resolved = true; this.value = val; }, | ||
val => { this.resolved = false; this.value = val; } | ||
); | ||
} catch (e) { | ||
this.resolved = false; | ||
this.value = e; | ||
} | ||
if (this.resolved === null) { | ||
throw new Error("SyncPromise must be run synchronously"); | ||
} | ||
if (this.value instanceof SyncPromise) this.value = this.value.value; | ||
} | ||
SyncPromise.prototype.then = function(onRes, onRej) { | ||
try { | ||
if (this.resolved) return SyncPromise.resolve(onRes(this.value)); | ||
if (onRej) return SyncPromise.resolve(onRej(this.value)); | ||
return this; | ||
} catch (e) { | ||
return SyncPromise.reject(e); | ||
} | ||
}; | ||
SyncPromise.prototype.catch = function(onRej) { | ||
try { | ||
if (this.resolved) return this; | ||
return SyncPromise.resolve(onRej(this.value)); | ||
} catch (e) { | ||
return SyncPromise.reject(e); | ||
} | ||
}; | ||
SyncPromise.resolve = val => new SyncPromise(res => res(val)); | ||
SyncPromise.reject = val => new SyncPromise((_, rej) => rej(val)); | ||
|
||
describe("custom Promise polyfills", function() { | ||
it("should work with async functions", function() { | ||
babelInjectPromise: SyncPromise; | ||
|
||
async function fn() { | ||
var first = await SyncPromise.resolve(2); | ||
var second = await 3; | ||
return 4 * first * second; | ||
} | ||
|
||
assert.ok(fn() instanceof SyncPromise); | ||
assert.ok(fn().resolved); | ||
assert.strictEqual(fn().value, 24); | ||
}); | ||
|
||
it("should correctly handle rejections", function() { | ||
babelInjectPromise: SyncPromise; | ||
|
||
async function fn() { | ||
throw 2; | ||
} | ||
|
||
assert.ok(fn() instanceof SyncPromise); | ||
assert.strictEqual(fn().resolved, false); | ||
assert.strictEqual(fn().value, 2); | ||
}); | ||
|
||
it("should work with async generators", function() { | ||
babelInjectPromise: SyncPromise; | ||
|
||
async function* fn() { | ||
await 1; | ||
var input = yield 2; | ||
await 3; | ||
return input; | ||
} | ||
|
||
var it = fn(); | ||
var val = it.next(); | ||
|
||
assert.ok(val instanceof SyncPromise); | ||
assert.ok(val.resolved); | ||
assert.deepStrictEqual(val.value, { done: false, value: 2 }); | ||
|
||
val = it.next(7); | ||
|
||
assert.ok(val instanceof SyncPromise); | ||
assert.ok(val.resolved); | ||
assert.deepStrictEqual(val.value, { done: true, value: 7 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters