Skip to content

Instantly share code, notes, and snippets.

@rbuckton
Last active July 20, 2022 11:57
Show Gist options
  • Save rbuckton/aa4ab629a8b0b97ee428 to your computer and use it in GitHub Desktop.
Save rbuckton/aa4ab629a8b0b97ee428 to your computer and use it in GitHub Desktop.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
return new Promise(function (resolve, reject) {
generator = generator.call(thisArg, _arguments);
function cast(value) { return value instanceof Promise && value.constructor === Promise ? value
: new Promise(function (resolve) { resolve(value); }); }
function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
function step(verb, value) {
var result = generator[verb](value);
result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
}
step("next", void 0);
});
};
function F() {
return __awaiter(this, void 0, Promise, function* () {
yield someAsync();
return 1;
});
}
function G() {
return __awaiter(this, void 0, MyPromise, function* () {
yield someAsync();
return 1;
});
}
function H(x) {
var f = () => __awaiter(this, arguments, Promise, function* (_arguments) { yield _arguments[0]; });
}
declare function someAsync(): Promise<string>;
async function F() { // Promise<number>
await someAsync(); // string
return 1;
}
async function G(): MyPromise<number> {
await someAsync(); // string
return 1;
}
function H(x: Promise<number>) {
let f = async () => await arguments[0];
}
@JsonFreeman
Copy link

To clarify the two key differences between this and the previous design are:

  1. The call to cast, which wraps an awaited non-promise value in a promise so that the generator is resumed on a later turn.
  2. None of the intermediate promises are resolved with another promise (except the final return value if it's a promise).

Correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment