I would like to execute functions one at a time and call another function when a function is finished. I was able to do it using callbacks but not using a promise chain. Here is what I tried (based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#chained_promises) but it executes the first 3 functions at the same time instead of waiting 1 second in each function:
function displayAll() { var myPromise = (new Promise(display1)) .then((new Promise(display2)) .then((new Promise(display3)) .then(display4))); } function display1(resolve) { setTimeout(function () { console.log("display1"); resolve(); }, 1000); } function display2(resolve) { setTimeout(function () { console.log("display2"); resolve(); }, 1000); } function display3(resolve) { setTimeout(function () { console.log("display3"); resolve(); }, 1000); } function display4(resolve) { setTimeout(function () { console.log("display4"); }, 1000); } Do you know what is wrong with the code and if it is possible to do what I am trying to do without callbacks?
https://stackoverflow.com/questions/66164973/promise-chain-does-not-wait-until-other-promise-is-resolved February 12, 2021 at 08:35AM
没有评论:
发表评论