Я пытаюсь переложить обещания, но второй не вызывает функцию разрешения. Что я делаю неправильно?ECMAScript 6 Цепочные обещания
function getCustomers(){ let promise = new Promise( function (resolve, reject){ console.log("Getting customers"); // Emulate an async server call here setTimeout(function(){ 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( function (resolve, reject){ console.log("Getting orders"); // Emulate an async server call here setTimeout(function(){ 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");
Кодовые печатает "Получение заказов" от второй функции, но не печатает "Order 123":
Получение клиентов Цепные GetCustomers и getOrders. Ожидание результатов Получение заказов
Обновление. Я хотел вставить печать на консоль между цепными методами, которые возвращают обещания. Я думаю, что-то подобное не представляется возможным:
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));
Спасибо, Берги. –
Я думаю, что невозможно вставить «затем», который просто печатает на консоли между цепями, которые возвращают обещания. –
Отлично! Мне не хватало «возврата»; в первой печати, которая разрушала цепь. Спасибо, @Bergi –