JavaScript Tutorial
  • 🍼Introduction
  • 🥛Hello Gamers
  • 🍯Variables
    • Action Game Example
  • 🌰Strings
    • RPG Example
  • 🍪Character Acces
    • Simulation Example
  • 🍩Plus (+) operator
    • Strategy Game Example
  • 🥜Functions
    • Adventure Game Example
  • 🍿Template strings
    • Strategy Game Example
  • 🍫Numbers
    • Sports Game Example
  • 🍬Converting Numbers to Strings
    • Fighting Game Example
  • 🍭Operations
    • FPS Game Example
  • 🍮Conditions
    • Platformer Game Example
  • 🎂Arrays
    • Puzzle Game Example
  • 🍰Objects
    • Racing Game Example
  • 🧁Arrow Functions
    • Rhythm Game Example
  • 🔌Shameless Plug
  • 🥧Implicit Return
    • Platform Game Example
  • 🍦String Methods
    • GRID Game Example
  • 🍨Object Literals
    • Board Game Example
  • 🍧DynamicProperty
    • Political Game Example
  • 🍡Reading Dynamic Property
    • Hide & Seek Game
  • 🥮Objects Continued
    • Medieval Game Example
  • 🥠Object Shorthands
    • Trivia Game example
  • 🔌Shameless Plug (again)
  • 🍥Destructuring & Concatenation
    • Dig a hole game example
  • 🍏Optional Chaining
    • Action Game Example
  • 🍐Nullish coalescing operator
    • Adventure Game Example
  • 🍊Refactoring If Conditions
    • Arcade Game Example
  • 🍋Implicit Conversion & Falcy Values
    • Strategy Game Example
  • 🍌Arrays of objects
    • Sports Game Example
  • 🍉Transforming Array of Objects
    • Fighters Game Example
  • 🍇Some important array methods
    • FPS Example
  • 🫐Reducing arrays of objects
    • Third Person Shooter Example
  • 🍓Try....catch
    • Platformer Example
  • 🍈Immutability
    • Puzzle game example
  • 🍒Creating new arrays or objects
    • Racing Game Example
  • 🔌Shameless Plug (again)
  • 🍑Immutable object operations
    • Basics
    • Rhythm Game Example
  • 🥭Classes
    • Basics
    • Platformer Game Example
  • 🍍Instance Method
    • Basics
    • Role-playing game example
  • 🥥Object Oriented Programming
    • Basics
    • Stealth Game Example
  • 🥝Getters & Setters
    • Basics
    • Survival Game Example
  • 🍅Static Methods & Chaining
    • Basics
    • Tactical Role Playing Game
  • 🍆Class Inheritance
    • Tower Defense Game Example
  • 🥑Use of Super
    • Vehicular Combat Game example
  • 🫒Public Class Field
    • MMORPG Example
  • 🥦Private class fields
    • Battle Royale Games Example
  • 🥬Aysnc/Await
    • Collectible Card Game example
    • Video Board game example
  • 🫑SetTimeout
    • Dice game example
  • 🥒Execution Order
    • Digital Collectible Card Game example
  • 🌶️The callback pattern
    • Life Simulation Game example
  • 🌽Promises
    • Music Game Example
  • 🥕Writing a function that returns a promise
    • Party game example
  • 🧄Introduction to JSON
    • Tile based game example
  • 🧅JSON.parse(string) / JSON.stringify(object)
    • Rhythm action game example
  • 🍠JSON Example for a game
    • Fetching JSON game data
  • 🥐Handling fetch errors
    • Fetch and HTTP Requests
  • 🥯ES Modules
    • Tower Defence Game Example
  • 🍞Default Export
    • Strategy Adventure Game Example
  • 🥖Fetch Wrapper
    • Strategy Role-playing game example
  • 🔌Final Shameless Plug
  • 🫓Lexical Scope and Arrow Functions
    • Vehicular Combat Game Example
  • 🥨Passing Functions
    • Vehicular simulation game example
  • 🥚Dynamic Imports
    • Video card game example
  • 🍠What is a closure
    • Video casino game example
  • 🎆Congratulations
Powered by GitBook
On this page

Promises

In JavaScript, a promise is an object that represents the result of an asynchronous operation. A promise can be in one of three states:

  • Pending: The asynchronous operation has not completed yet.

  • Fulfilled: The asynchronous operation has completed successfully.

  • Rejected: The asynchronous operation has failed.

A promise is created using the Promise constructor, and is passed a function called the "executor" function. The executor function is responsible for starting the asynchronous operation, and it is called with two functions as arguments: resolve and reject.

The resolve function is called when the asynchronous operation succeeds, and the reject function is called when the operation fails.

Here is an example of a promise:

const promise = new Promise((resolve, reject) => {
  // Perform an asynchronous operation
  setTimeout(() => {
    // If the operation succeeds, call resolve
    resolve('Success');
  }, 1000);
});

promise.then(result => {
  // This function will be called when the promise is fulfilled
  console.log(result); // 'Success'
});

In this example, the promise is created with a setTimeout function as the asynchronous operation. The setTimeout function is set to complete after 1 second (1000 milliseconds), and when it does, it calls the resolve function with the value 'Success'.

The then method is used to specify a function to be called when the promise is fulfilled. In this case, the function logs the result of the asynchronous operation to the console.

Promises are often used to handle the results of asynchronous operations in a more organized and easier-to-read way. They provide a way to simplify the handling of asynchronous operations, and are a common pattern in JavaScript programming.

Here is an example of a promise using the then and catch methods:

const promise = new Promise((resolve, reject) => {
  // Perform an asynchronous operation
  setTimeout(() => {
    // If the operation succeeds, call resolve
    resolve('Success');
  }, 1000);
});

promise
  .then(result => {
    // This function will be called when the promise is fulfilled
    console.log(result); // 'Success'
  })
  .catch(error => {
    // This function will be called when the promise is rejected
    console.error(error);
  });

In this example, the then method is used to specify a function to be called when the promise is fulfilled, and the catch method is used to specify a function to be called when the promise is rejected.

The then function receives the result of the asynchronous operation as an argument, and the catch function receives the error that occurred.

If the asynchronous operation succeeds, the resolve function is called and the promise is fulfilled. The then function is called with the result of the operation, and the catch function is not called.

If the asynchronous operation fails, the reject function is called and the promise is rejected. The catch function is called with the error that occurred, and the then function is not called.

The then and catch methods allow you to handle the results of a promise in a more organized and easier-to-read way. They provide a way to simplify the handling of asynchronous operations, and are a common pattern in programming.

PreviousLife Simulation Game exampleNextMusic Game Example

Last updated 2 years ago

🌽