fix: gnc eslint error.

This commit is contained in:
Rintim 2024-04-17 13:15:11 +08:00
parent 196212fa39
commit c82ad68d32
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
1 changed files with 42 additions and 38 deletions

View File

@ -8,45 +8,49 @@ export class GNC {
* @returns
*/
of(fn) {
return this.is.generatorFunc(fn)
? /**
* @param {Parameters<typeof fn>} args
* @returns {Promise<ReturnType<typeof fn>>}
*/
function genCoroutine(...args) {
let gen = fn.apply(this, args);
gen.status = "next";
gen.state = undefined;
const callback = (resolve, reject) => {
let result,
nexts = resolve,
throws = reject;
try {
result = gen[gen.status](gen.state);
} catch (error) {
reject(error);
return;
}
if (!result.done) {
nexts = (item) => {
gen.state = item;
gen.status = "next";
callback(resolve, reject);
};
throws = (err) => {
gen.state = err;
gen.status = "throw";
callback(resolve, reject);
};
}
result = result.value;
Promise.resolve(result).then(nexts, throws);
/**
* @param {Parameters<typeof fn>} args
* @returns {Promise<ReturnType<typeof fn>>}
*/
function genCoroutine(...args) {
/**
* @type {Generator<unknown, any, unknown> & { status: "next" | "throw" , state?: any}}
*/
// @ts-expect-error Must Ok
let gen = fn.apply(this, args);
gen.status = "next";
gen.state = undefined;
const callback = (resolve, reject) => {
let result,
nexts = resolve,
throws = reject;
try {
result = gen[gen.status](gen.state);
} catch (error) {
reject(error);
return;
}
if (!result.done) {
nexts = (item) => {
gen.state = item;
gen.status = "next";
callback(resolve, reject);
};
return new Promise(callback);
}
: (() => {
throw new TypeError("gnc.of needs a GeneratorFunction.");
})();
throws = (err) => {
gen.state = err;
gen.status = "throw";
callback(resolve, reject);
};
}
result = result.value;
Promise.resolve(result).then(nexts, throws);
};
return new Promise(callback);
}
if (!this.is.generatorFunc(fn)) throw new TypeError("gnc.of needs a GeneratorFunction.");
return genCoroutine;
}
is = new Is();
}