Ahmad Naser Turnkey Ecosystem Ahmad Naser
May 21, 2019

Introduction to Promise in JavaScript

Published in the Ahmad Naser ecosystem. Estimated reading time: 1 minute.

The Promise object represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.

<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<div class="container">
  <div class="row" id="firstDay">
  </div></div>
$(document).ready(function(){

var promise1 = new Promise(function(resolve, reject) {

  $.ajax({
    url: 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22',
    dataType: 'json',
    success: function( data ) {
      alert( "SUCCESS:  " + data );

         resolve(data);
    },
    error: function( data ) {
      alert( "ERROR:  " + data );
      reject("data"); 
    }
  });
  



});

promise1.then(function(value) {
              var message = ("Weather: "+ value.weather[0].main);
        $('#firstDay').append(message);
}).catch((reason) => {
            console.log('Handle rejected promise ('+reason+') here.');
        });

console.log(promise1);
// expected output: [object Promise]





        
        
    });

Leave a Reply

Your email address will not be published. Required fields are marked *