Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About possible wrong returned value while 'Promise.resolve' #37998

Closed
FANGZHANG34 opened this issue Feb 6, 2025 · 1 comment
Closed

About possible wrong returned value while 'Promise.resolve' #37998

FANGZHANG34 opened this issue Feb 6, 2025 · 1 comment
Labels
closed: question A user misinterpretation or support question. No action required.

Comments

@FANGZHANG34
Copy link

FANGZHANG34 commented Feb 6, 2025

I wrote some codes like here:

const RealStory = new class RealStory{
	/**@type {RealStory[]} */
	static pages = [];
	static isClearing = false;
	static intervalId = setInterval(()=>(RealStory.isClearing || (
		RealStory.isClearing = true,RealStory.pages[0].clear().then(()=>RealStory.isClearing = false)
	)),50);
	static promise = class StoryPromise{
		/**
		 * 
		 * @this {StoryPromise}
		 * @param {(value)=>void} resolve 
		 * @param {(reason?)=>void} reject 
		 */
		static executor(resolve,reject){this.resolve = resolve,this.reject = reject;}
		/**@type {(value)=>void} */
		resolve;
		/**@type {(reason?)=>void} */
		reject;
		self = new Promise(StoryPromise.executor.bind(this));
	};
	newPage(){return new RealStory(this);}
	newPagePromise(){return Promise.resolve([new RealStory(this)]);}
	/**
	 * 
	 * @param {()=>*} fn 
	 */
	then(fn){return typeof fn === 'function' && this.fnList.unshift(fn),this;}
	getNextPage(){
		const temp = this.ofStory.pages;
		return temp[temp.indexOf(this) + 1];
	}
	getPreviousPage(){
		const temp = this.ofStory.pages;
		return temp[temp.indexOf(this) - 1];
	}
	newPromiseObj(){
		const temp = new RealStory.promise;
		return this.then(()=>temp.self),temp;
	}
	async clear(){
		var i = 0,temp = this;
		while(RealStory !== temp) temp = temp.ofStory,i++;
		while(this.pages.length || this.fnList.length){
			while(this.fnList.length) try{await this.fnList.pop()?.();}catch(e){console.error('Depth of the fn : '+i);console.error(e);}
			try{await this.pages.shift()?.clear?.();}catch(e){console.error('Depth of the page : '+i);console.error(e);}
		}
	}
	/**@type {RealStory} */
	ofStory;
	/**@type {RealStory[]} */
	pages = [];
	/**@type {(()=>*)[]} */
	fnList = [];
	constructor(ofStory){(this.ofStory = ofStory instanceof RealStory ? ofStory : RealStory).pages.push(this);}
}();

But when I wanted await RealStory, I got undefined unexpectedly.
Is here anybody to told me a possible reason? Thank you!

@github-actions github-actions bot added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 6, 2025
@Josh-Cena
Copy link
Member

This looks like a general programming question, not a problem with our content. Please ask in an open Q&A forum such as stack overflow instead.

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2025
@Josh-Cena Josh-Cena added closed: question A user misinterpretation or support question. No action required. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: question A user misinterpretation or support question. No action required.
Projects
None yet
Development

No branches or pull requests

2 participants