Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptEmber.js Promise then() Method

Ember.js Promise then() Method

Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.

The then() method allows you to execute code that depends on the successful completion of that asynchronous operation.

Syntax:

then(onResolved, onRejected, String label)

 

Parameters: It takes three parameters.

  •  Resolved callback: The function is executed when the promise is resolved.
  •  Rejected callback: The function is executed when the promise is rejected.
  •  String label:  This is an optional parameter that is basically used to label the promise.

Return Value:  It returns a new promise.

Steps to Install and Run Ember.js:

Step 1: To run the following examples you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:

npm install -g ember-cli

Step 2: Now you can create the project by typing in the following piece of code:

ember new <project-name> --lang en

Step 3: To start the server, type:

ember serve

Step 4: After the above query is executed successfully you can check your progress on 

http://localhost:4200/

Example 1:  Type the following code to use then() method to resolve the promise.

app/controller/app.js

Javascript




let p1 = new Promise(function (resolve) {
    setTimeout(function () {
        resolve('promise 1');
    }, 2000);
});
  
p1.then((value) => {
    console.log(value);
});


app/templates/application.hbs

Javascript




{{page-title "Emberjs"}}
  
<div>
    <h2>Welcome to the tutorial of Promises in Ember.js </h2>
</div>


Output: Visit localhost:4200/ to view the output

 

Example 2: Type the following code to use then() method to reject the promise.

apps/app1.js

Javascript




let p2 = new Promise((reject) => {
    console.log('Promise is pending');
    setTimeout(() => {
        reject(new Error('I am an error'));
    }, 2000);
});
  
p2.then(
    (v) => {
        console.log(v);
    },
    (error) => {
        console.log(error);
    }
);


app/templates/application.hbs

Javascript




{{page-title "Emberjs"}}
  
<div>
    <h2>Welcome to the tutorial of Promises in Ember.js </h2>
</div>


Output: Visit localhost:4200/ to view the output

 

Reference: https://api.emberjs.com/ember/3.18/classes/Promise/methods/then?anchor=then

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments