Free Web Development Online Tutorials, Learn to Code
JavaScript Promises to handle asynchronous operations and callbacks, Promises Async Await in JavaScript

Promises JavaScript Example

In this JavaScript Promise tutorial you will learn how to use "Promise" in JavaScript Code

Note:

JavaScript Promise is the new features of advanced JavaScript (ES6), specially used when you want to write async JavaScript function in Node application development, so to learn this feature you may need to have Visual Studio Code or something similar editor.

What is JavaScript Promise?

A promise is a single object value, which will indicate the state of any JavaScript call, there can be three different possibilities.

  1. Completed
  2. Rejected (if rejected, then it may contain the reason)
  3. Pending (future possibility)
Basically it says, a promise of doing whatever task is given.

JavaScript Promises are used to manage multiple asynchronous operations where callbacks can call another function.

Here is an example of javascript function that will returns promise, which will resolve after a specified time duration.

const wait = time => new Promise((resolve) => setTimeout(resolve, time));
wait(5000).then(() => console.log('Hello World!')); 

Here is another promise example.

var promise = new Promise(function(resolve, reject) 
{
  const a = "JavaScript";
  const c = "WebTrainingRoom";
  if(a === c) {
    resolve();
  } else {
    reject();
  }
});
  
promise.
    then(function () {
        console.log('Success, Well Done'); 
    }).
    catch(function () {
        console.log('There are some error');
    });
Comment
Name
Email
Website
Subscribe
 
javaScript promises example


javascript Interview Questions Answers


JavaScript Examples | JavaScript Online Course