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

Async,Await Promise

Susan Sarandon
Release: 2024-12-20 06:28:17
Original
346 people have browsed it

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);
Copy after login

} 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!

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template