const promise = new Promise((resolve, reject) => {
// contain an operation
// ...
// fake async api call and return the state
setTimeout(() => {
if (success) {
resolve([
{ username: 'john', email: 'john@test.com' },
{ username: 'jane', email: 'jane@test.com' },
]);
} else {
reject('Failed to the user list');
}
}, 1000);
});
// after setTimeout executes -> resolve/callback with user data
getUsers().then((users) => {
console.log(users);
});
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter