JavaScript How can I eliminate these nested Promises?

I've read that you should avoid nested promises in JavaScript, as they tend to be an antipattern, but I'm having trouble figuring out how to avoid them in my particular use case. Hopefully someone with more experience than me can see where I'm going wrong? Any advice would be much appreciated!!

Essentially, I'm retrieving some data asynchronously, processing it and catching a possible Error, and then saving some data asynchronously. Here's a much-simplified example:

class Foo {
  changeName(path, newName) {
      this.getPage(path) // AJAX call, returns a promise with type Page,
                         //  may throw an Error if the page does not exist.
        .then(page=> {
          // Some modifications are made to page, omitted here
          return page
        })
        .catch(e=>{
          if(e instanceof PageDoesNotExistError) {
            return new Page();
          }
        })
        .then(page=> {
          page.name = newName;
          this.savePage(path, page); // ******
          // I want my outer changeName method to return this ^ promise,
          // or at least a promise that will resolve when this promise
          // resolves, with the same value.
        })
  }
}

How can I have changeName return a promise that will resolve with the value of this.savePage (the line marked with //******), so that I could do something like this elsewhere:

myFooInstance.changeName('/path', 'New Name').then(page=> {
  // Do something with the returned saved page
});
Answer:1

I'm building a web app that has to work on very old browsers (IE 8, Firefox 20, etc). Can I rely on document.readyState being interactive and complete working correctly? Basically, I want to perform ...

I'm building a web app that has to work on very old browsers (IE 8, Firefox 20, etc). Can I rely on document.readyState being interactive and complete working correctly? Basically, I want to perform ...

I'm trying to look for certain elements if they contain a certain class in a Wordpress Widget. But what I can't understand is why the authors.hasClass() is OK, but the currentAuthor.hasClass() throws ...

I'm trying to look for certain elements if they contain a certain class in a Wordpress Widget. But what I can't understand is why the authors.hasClass() is OK, but the currentAuthor.hasClass() throws ...

  1. jquery hasclass doesn't work
  2. jquery doesn't hasclass
  3. jquery does not has class
  4. jquery hasclass does not work

This might sound a stupid question, but I'm trying to pix this past...I don't know how many hours. I have a jquery combobox here. Combobox code :- <script> (function( $ ) { $.widget( "...

This might sound a stupid question, but I'm trying to pix this past...I don't know how many hours. I have a jquery combobox here. Combobox code :- <script> (function( $ ) { $.widget( "...

  1. onchange event combobox javascript
  2. onchange event combobox
  3. combobox onchange event
  4. event onchange combobox php
  5. onchange event in extjs combobox
  6. jquery onchange combobox event
  7. combobox onchange event in c#
  8. combobox onchange event access

i need help. I have a project and i need duplicate html nodes dynamically. Those nodes have ng-bind and ng-model attributes like this: <input class="quest_model" type="text" data-ng-model="quest"&...

i need help. I have a project and i need duplicate html nodes dynamically. Those nodes have ng-bind and ng-model attributes like this: <input class="quest_model" type="text" data-ng-model="quest"&...