Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ECMAScript 6 Chaining Promises

I'm trying to chain promises, but the second one doesn't call the resolve function. What do I do wrong?

function getCustomers(){

  let promise = new Promise((resolve, reject) => {
      console.log("Getting customers");
      // Emulate an async server call here
      setTimeout(() => {
        var success = true;
        if (success) {
          resolve( "John Smith"); // got the customer
        } else {
          reject("Can't get customers");
        }
      }, 1000);

     }
  );
  return promise;
}

function getOrders(customer) {

  let promise =  new Promise((resolve, reject) => {
      console.log("Getting orders");
      // Emulate an async server call here
      setTimeout(() => {
        var success = true;
        if (success) {
          resolve("Order 123"); // got the order
        } else {
          reject("Can't get orders");
        }
      }, 1000);

     }
  );
  return promise;
}

getCustomers()
  .then((cust) => getOrders(cust))
  .catch((err) => console.log(err));

console.log("Chained getCustomers and getOrders. Waiting for results");

The code prints "Getting orders" from the second function, but doesn't print "Order 123":

Getting customers Chained getCustomers and getOrders. Waiting for results Getting orders

Update. I wanted to insert the print on the console between chained methods that return promises. I guess something like this is not possible:

getCustomers()
  .then((cust) => console.log(cust))  //Can't print between chained promises?
  .then((cust) => getOrders(cust))  
  .then((order) => console.log(order))
  .catch((err) => console.error(err));
like image 900
Yakov Fain Avatar asked Apr 14 '26 09:04

Yakov Fain


1 Answers

You want to chain a success handler (for your resolve result "Order 123"), not an error handler. So use then instead of catch :-)

getCustomers()
  .then(getOrders)
  .then((orders) => console.log(orders))
  .catch((err) => console.error(err));

None of the promises was rejected, so the console.log(err) in your code was never called.

I wanted to insert the print on the console between chained methods that return promises. I guess something like this is not possible:

getCustomers()
  .then((cust) => console.log(cust))  //Can't print between chained promises?
  .then((cust) => getOrders(cust))

Yes it is possible, but you are intercepting a chain here. So the second then callback actually is not called with cust, but with the result of the first then callback - and console.log returns undefined, with which getOrders will get some problems.

You'd either do

var customers = getCustomers();
customers.then(console.log);
customers.then(getOrders).then((orders) => …)

or simpler just

getCustomers()
  .then((cust) => { console.log(cust); return cust; })
  .then(getOrders)
  .then((orders) => …)
like image 113
Bergi Avatar answered Apr 18 '26 08:04

Bergi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!