Home >Web Front-end >JS Tutorial >Async,Await Promise

Async,Await Promise

Susan Sarandon
Susan SarandonOriginal
2024-12-20 06:28:17373browse

Async,Await Promise

function asyncTask(delay, result) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(result);
}, delay);
});
}

const runtsk = async () => {
try {
const result = await Promise.all([
asyncTask(3000, 'first call'),
asyncTask(2000, 'second call'),
asyncTask(1000, 'third call')
]);

// Log the results of all the asynchronous tasks
console.log(result);

} catch (error) {
console.log('Error:', error);
}
};

runtsk();

The above is the detailed content of Async,Await Promise. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn