Promise behaviour in javascript nativescript

nativescriptcore

#1
var observableModule = require("data/observable");

function HomeViewModel() {
  var viewModel = observableModule.fromObject({
    onButtonTap: function () {

      var daysPrior = 1;

      new Promise(function (resolve, reject) {

        var date = new Date();
        var today = parseInt(date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
        console.log(today);
        resolve(today);

      }).then(function (result) {

        var date1 = new Date(result);
        date1.setDate(date1.getDate() - daysPrior);
        var promise = date1 = parseInt(date1.getMonth() + 1) + '/' + date1.getDate() + '/' + date1.getFullYear();
        console.log(date1);

        return date1;

      }).then(function (result) {

        var date2 = new Date(result);
        date2.setDate(date2.getDate() - daysPrior);
        var date2 = parseInt(date2.getMonth() + 1) + '/' + date2.getDate() + '/' + date2.getFullYear();
        console.log(date2);
        return date2;

      }).then(function (result) {

        var date3 = new Date(result);
        date3.setDate(date3.getDate() - daysPrior);
        var date3 = parseInt(date3.getMonth() + 1) + '/' + date3.getDate() + '/' + date3.getFullYear();
        console.log(date3);
        return date3;

        }).then(function (result) {

          var date4 = new Date(result);
          date4.setDate(date4.getDate() - daysPrior);
          var date4 = parseInt(date4.getMonth() + 1) + '/' + date4.getDate() + '/' + date4.getFullYear();
          console.log(date4);
          return date4;

        }).catch(function (error) {
        console.log(error);
        })
      
    },


  });

  return viewModel;
}

module.exports = HomeViewModel;

and the output when i tap button for first time is

	
[Redmi]: 9/20/2018
[Redmi]: 9/18/2018
[Redmi]: 9/16/2018
[Redmi]: 9/19/2018
[Redmi]: 9/17/2018

then it will work fine for next tap… but why???

	
[Redmi]: 9/20/2018
[Redmi]: 9/19/2018
[Redmi]: 9/18/2018
[Redmi]: 9/17/2018
[Redmi]: 9/16/2018

check here


#2

If you are just comparing with Playground’s console, it may be a false alarm. The logs from device are transmitted to your browser console and it may loose the right order some times.


#3

yeah i checked in my device and its working perfect. but not in browser… thank you.