Creating Promises in JavaScript

From the Async JavaScript cheat sheet · Promises · verified Jul 2026

Creating Promises

Create a new Promise that resolves or rejects based on asynchronous operations

javascript
// Create a promise
const promise = new Promise((resolve, reject) => {
  if (success) resolve(value);
  else reject(error);
});

// Using promises
promise
  .then(result => console.log(result))
  .catch(error => console.error(error))
  .finally(() => console.log('Done'));

// Promise from callback
function promisify(fn) {
  return (...args) => new Promise((resolve, reject) => {
    fn(...args, (err, data) => {
      if (err) reject(err);
      else resolve(data);
    });
  });
}
💡 A Promise is an object representing a future value that may not be available yet
⚡ Promises have three states: pending, fulfilled (resolved), or rejected
✅ Better than callbacks for handling async operations and avoiding callback hell
🔄 Once settled (fulfilled or rejected), a promise state cannot change

More JavaScript tasks

Back to the full Async JavaScript cheat sheet