1

This is more of a syntax/readability question. Let's say I have this:

  ProductsService.getProducts()
    .then(function (products) {
      model.products = products;
    }, function(){
      //handle error
    });

If the promise is resolved, I populate model.products with the result of the promise. Is there a way to organize this code so it's more like:

model.products = //result of promise

I prefer having the variable I'm setting front and center instead of having it buried in that promise.

Paul Erdos
  • 1,295
  • 2
  • 19
  • 42

2 Answers2

0

In ES6 and below this is not possible.

As an aside, in ES7 we can use await in combination with an async function to achieve the type of 'readable asynchronous code' that you are looking for:

ProductsService.getProducts = async function() {
    // returns a promise...
};

model.products = await ProductsService.getProducts();

If you really are desperate to use it, the functionality is available using Babel.

sdgluck
  • 18,456
  • 4
  • 56
  • 83
0

You could do something like this:

function setProduct(products) {
    model.products = products;
}

function foo(){
    return ProductsService.getProducts();
}

foo().then(setProduct);

But as said before, you have to wait for ES7 or use typescript/babel

romuleald
  • 1,282
  • 13
  • 27