[Vuejs]-Open window in new tab after async api call

0πŸ‘

In an ideal world, all asynchronous functions would already return promises.
But there may be still old libraries that use await.
You can create a Promise that wraps the await function as below:

const delay = (ms) => new Promise(resolve => setTimeout(resolve.bind(null, ms/1000), ms));


delay(3*1000).then((sec) => alert(sec + " seconds passed"));

0πŸ‘

Simulate a click event like this after your api call resolves:

const someFunction = async () => {
  const value = await api.someCallToApi();
  const link = document.createElement('a');
  link.href = 'https://example.com';
  link.target = '_blank';
  link.click();
  link.remove();
}

Leave a comment