DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. JavaScript: How to Define and Process a Promise Object

JavaScript: How to Define and Process a Promise Object

In this post, I'll provide direction and code samples on how to create and use a Promise Object in JavaScript. Read on to see how it's done.

Ajitesh Kumar user avatar by
Ajitesh Kumar
CORE ·
Feb. 18, 16 · Tutorial
Like (8)
Save
Tweet
Share
11.15K Views

Join the DZone community and get the full member experience.

Join For Free

This article provides tips and code samples on how to define and process a Promise object. Please feel free to comment/suggest if I failed to mention one or more important points. 

How to Define a Promise Object

The following example demonstrates an Auth module within an API login that returns a Promise that will either be resolved/fulfilled or rejected.  The returned object, will represent a domain object such as User or Error object with a status and error message, as demonstrated below:

// Promise needs to be imported when executing a module using Node
//
var Promise = require("promise");

function Auth() {
}

Auth.prototype.login = function(user) {
    return new Promise(function(resolve, reject) {
        if(user.username != "" && user.password != "") {
            resolve({username: user.username, age: 21, location: "Hyderabad", firstname: "Raju", lastname: "Mastana"});
        } else {
            if(user.username == ""){
                reject({status:1, message: "Username is invalid"});
            } else {
                reject({status:2, message: "Password is invalid"});
            }
        }
    });
}

module.exports = Auth;

How to Process a Promise Object

The following tasks are defined in the code sample provided, which calls login API on an Auth object:

  • Create an Auth object
  • Invoke login API on Auth object
  • Invoke "then" method on Promise object returned from the invocation of login API.
// Import the Auth module
//
var Auth = require('./Auth');

// Create an Auth object
//
var auth = new Auth();
//
// Calls login API on Auth object
// Returns a promise object
//
var loginPromise = auth.login({username:'', password:'pass123'});
//
// Once the state of Promise is fulfilled/resolved or rejected,
// following is executed
//
loginPromise.then(function(response){
    console.log("User name: " + response.name);
}, function(error) {
    console.log(error.message);
});
Object (computer science) JavaScript

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • Load Balancing Pattern
  • Distributed SQL: An Alternative to Database Sharding
  • Real-Time Stream Processing With Hazelcast and StreamNative

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: