JavaScript javascript : promise issue

This is my code.

function m1() {
  return new Promise(function(resolve, reject) {
    var files = $('#aws-upload-files')[0].files;
    if (files.length > 0) {
      for (var i = 0; i < files.length; i++) {
        loadFile(files[i], $('#filecategory' + i + ' option:selected').text());
      }
    }
  })
}

function submitFileUpload() {
  $('#aws-upload-submit').click(function() {
    m1().then(function(result) {
      console.log('~~~ success here ');
      window.location = 'https://example.com/';
    })
      .catch(function(error) {
      console.log('Error');
    })
  })
}

$(function() {
  submitFileUpload();
})

When button "#aws-upload-submit" is clicked, the function m1() is called.

The function m1() returns a promise.

In .then() I check whether the promise has returned a success and if it is success, I redirect the browser to https://example.com

In my case when the button is clicked, the m1() gets called and the function works successfully but no promise is getting created or .then() is not working at all.

I do not see any console.log stating "~~~ success here" at all even though m1() is working as expected.

Can anyone tell me what is wrong with my promise ?

Answer:1

you need to resolve promise or reject it , and so you have to do code like this

enter image description here

Read : Promise

return new Promise(function(resolve, reject) {
        //code 
       resolve();
 }

function m1() {
  return new Promise(function(resolve, reject) {
    var files = $('#aws-upload-files')[0].files;
    if (files.length > 0) {
      for (var i = 0; i < files.length; i++) {
        loadFile(files[i], $('#filecategory' + i + ' option:selected').text());
      }
       resolve();
    } else {
       reject();
    }
  })
}


m1().then(
  () => console.log("Task Complete!"),
  () => console.log("Task Errored!"),
);
Answer:2

Since loadFile seems to be sync, there's no point in using a Promise here - simply return success or failure from m1 directly.

function m1() {
  var files = $('#aws-upload-files')[0].files;
  if (files.length < 0) return false;
  for (var i = 0; i < files.length; i++) {
    loadFile(files[i], $('#filecategory' + i + ' option:selected').text());
  }
  return true;
}
const succeeded = m1();
console.log(succeeded ? "Task Complete!" : "Task Errored!")
Answer:3

In the callback of new Promise you are not resolving not rejecting the promise, so your promise keeps waiting for it

You need to do something like this

function m1() {

    return new Promise(function(resolve, reject) {
        var files = $('#aws-upload-files')[0].files;

        if (files.length > 0) {
            for (var i = 0; i < files.length; i++) {
                loadFile(files[i], $('#filecategory' + i + ' option:selected').text());
            }
            resolve("loaded")
        } else {
            reject("no files")
        }
    })

}
Answer:4

I have this donut chart currently working in an AngularJS app: But the design mockup says we would like this, note the border-radius property on the green portion of the arc: How do I add a border-...

I have this donut chart currently working in an AngularJS app: But the design mockup says we would like this, note the border-radius property on the green portion of the arc: How do I add a border-...

  1. border radius property in css
  2. border radius property in bootstrap 4
  3. border radius property
  4. border-radius property not working
  5. border radius property for all browsers
  6. border radius property in react native
  7. border radius property not working in ie
  8. border-radius property mdn
  9. css radius property
  10. border radius invalid property value
  11. border radius shorthand property
  12. css border radius property for all browsers
  13. border-radius the property is marked invalid
  14. css3 border-radius property

I am using bootstrap datetimepicker have to get the datetime and add days to it var get_date_time = Friday, October 23rd 2015, 12:00:00 am format get_date_time to oct 25 2015 12:00:00 am how to ...

I am using bootstrap datetimepicker have to get the datetime and add days to it var get_date_time = Friday, October 23rd 2015, 12:00:00 am format get_date_time to oct 25 2015 12:00:00 am how to ...

I have a simple list that binds two-way to a parameter hero in hero.component.ts. Though when I start typing in the input field it loses focus and I have to click it again. What am I supposed to do ...

I have a simple list that binds two-way to a parameter hero in hero.component.ts. Though when I start typing in the input field it loses focus and I have to click it again. What am I supposed to do ...

  1. input loses focus react
  2. input loses focus when typing react
  3. input loses focus when typing
  4. input loses focus when typing angular
  5. input loses focus
  6. input loses focus angular
  7. input loses focus on click
  8. input loses focus react native
  9. input loses focus javascript
  10. input loses focus angularjs
  11. input loses focus html
  12. react input loses focus on change
  13. react input loses focus on rerender
  14. input field loses focus
  15. react input loses focus on render
  16. react input loses focus on setstate
  17. vue input loses focus
  18. formik input loses focus
  19. jquery input loses focus
  20. ngfor input loses focus

I found that ng-init is not updated after rowIndex is updated. <div ng-init="item = getItem(rowIndex)"> {{ item.Name }} </div> item is not updated after rowIndex is changed (even if ...

I found that ng-init is not updated after rowIndex is updated. <div ng-init="item = getItem(rowIndex)"> {{ item.Name }} </div> item is not updated after rowIndex is changed (even if ...

  1. update-initramfs force