Simple code for asynchronously making updates before allowing further processing (c/o ChatGPT)
posted 2023.04.13 by Clark Wilkins

First, here's the “use case”. In the project I am working on now, many items that are being created can have a number of hashtags applied to them after creation, but there's often some followup action after that — such as loading the newly created item in a new page. The problem I had was that the new page was loading before the tagging was completed, and this resulted in errors.

I turned (again) to ChatGPT and got this fantastic explanation.

Me: “Using Javascript, inside a function, call another function that maps an object and performs an async operation. Wait for promises to resolve, then switch to a new page.”

// Define the function that maps an object and performs an async operation
async function asyncOperation(item) {
// Perform some async operation
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(`Async operation completed for item ${item.id}`);
return item;
}

// Define the function that calls the async operation on each item in an array
async function mapAndPerformAsyncOperation(array) {
const promises = array.map(item => asyncOperation(item));
const results = await Promise.all(promises);
console.log('All async operations completed');
return results;
}

// Define the function that switches to a new page
function switchToNewPage() {
console.log('Switching to new page');
// Replace "example.com" with the URL of your new page
window.location.replace('https://example.com');
}

// Define the main function that calls the other functions and waits for promises to resolve
async function main() {
const items = [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' },
];

const results = await mapAndPerformAsyncOperation(items);
console.log('All promises resolved');
switchToNewPage();
}

// Call the main function to start the process
main();

In this example, we first define the asyncOperation function that performs some async operation on an item and returns it. We then define the mapAndPerformAsyncOperation function that calls asyncOperation on each item in an array and returns an array of results.

Next, we define the switchToNewPage function that switches to a new page. Finally, we define the main function that calls mapAndPerformAsyncOperation, waits for all promises to resolve using Promise.all, and then calls switchToNewPage.

When you call the main function, it will log the progress to the console and switch to the new page once all async operations have completed. Note that in this example, the async operation is simulated using setTimeout, but you can replace this with your own async operation.

There are a couple of points to make here. First, look at the original question and ask yourself how you can effectively do the same thing in a standard (Google) search query. Good luck!!

Second, the explanation is clear, and the functions are labeled in a manner that explains exactly what they do. This is a game-changer.

Ok, and for the record, I wanted to end up with a single reusable function that I could import into any component that needs it.

// apply an arbitrary set of hashtags to the specified item (used mostly when creating items)

export async function mapDesiredTags( applyHashtags, itemId, tagType ) {

async function tagItem( tag, itemId, tagType ) {

await new Promise( resolve => { resolve( setTag ( tagType, itemId, tag ) ) } );
return tag;

}

const promises = applyHashtags.map( tag => tagItem( tag, itemId, tagType ));
const results = await Promise.all( promises );
console.log( 'applied tags: ', results );
return null;

}

And here's a sample deployment on a component that has imported mapDesiredTags

await mapDesiredTags( applyHashTags, requestId, 5 ); // tag the new request

Problem solved thanks to ChatGPT :-)