return a simple promise

It’s easy but I keep forgetting. I have a function that triggers an axios request and returns it as a promise – unless the value is already there. In that case I want to return a simple promise with just the value. The solution is Promise.resolve

async getSomeData(){
	if(this.dataStored !== null) {
		return Promise.resolve(this.dataStored);
	}
	
	return axios.get('/api/someurl')
		.then(response => {
			this.dataStored = response.data;
			return response.data;
		});
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.