w3resource

How to create a simple JavaScript Promise


Create a Simple Promise:

Write a JavaScript function that returns a Promise that resolves with a "Hello, World!" message after 1 second.

Solution-1: Using a Basic Promise

Code:

// Define a function that returns a Promise
function simplePromise() {
  // Create and return a new Promise
  return new Promise((resolve) => {
    // Use setTimeout to delay execution for 1 second
    setTimeout(() => {
      resolve("Hello, World!"); // Resolve the Promise with the message
    }, 1000); // 1000ms = 1 second
  });
}

// Call the function and handle the Promise
simplePromise().then((message) => {
  console.log(message); // Logs "Hello, World!"
});

Output:

"Hello, World!"

Explanation:

  • The simplePromise function returns a Promise object.
  • Inside the Promise, setTimeout is used to delay the resolve function for 1 second.
  • When the timer completes, the Promise resolves with the message "Hello, World!".
  • .then() is used to handle the resolved value and log it to the console.

Solution-2: Using Async/Await

Code:

// Define a function that returns a Promise
function simplePromise() {
  // Create and return a new Promise
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Hello, World!"); // Resolve the Promise with the message
    }, 1000); // 1 second delay
  });
}

// Define an async function to call the Promise
async function asyncFunction() {
  // Wait for the Promise to resolve and store the result
  const message = await simplePromise();
  console.log(message); // Logs "Hello, World!"
}

// Call the async function
asyncFunction(); 

Output:

"Hello, World!"

Explanation:

  • The simplePromise function is the same as in Solution 1.
  • An asyncFunction is defined to use await, which pauses execution until the Promise resolves.
  • The resolved value is stored in the message variable and logged to the console.

See the Pen promises-and-async-await-exercise-1 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that returns a Promise resolving with a random number after a delay based on that number’s value.
  • Write a JavaScript function that returns a Promise resolving with an array of strings after a dynamically computed timeout.
  • Write a JavaScript function that randomly resolves with "Success" or rejects with "Error" after a 500ms delay.
  • Write a JavaScript function that returns a Promise resolving with an object containing a timestamp and a custom message after a calculated delay.

Improve this sample solution and post your code through Disqus

Previous: JavaScript Promises Async/Await Exercises Home.
Next: How to use Promise chains for Sequential Async tasks?.

➤ Home: JavaScript Promises Async/Await Exercises

➤ Home: JavaScript Exercises

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.