fix: gnc eslint error.
This commit is contained in:
parent
196212fa39
commit
c82ad68d32
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue