在JavaScript中循环通过一系列承诺

发布于 2025-02-13 10:13:37 字数 1165 浏览 0 评论 0原文

我有一些代码可以通过一系列承诺循环,并输出值。

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

for (var promise of promises) {
  promise.then(seconds => console.log(`waited ${seconds} seconds`));
}

这样的问题是,承诺结果不会以数组的顺序记录。我的预期结果是:

Waited 1 seconds
Waited 3 seconds
Waited 2 seconds
Waited 4 seconds
Waited 5 seconds

结果是:

Waited 1 seconds
Waited 2 seconds
Waited 3 seconds
Waited 4 seconds
Waited 5 seconds

所以我想拥有这样的事情,

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

for (var promise of promises) {
  // When the promise is resolved, log `Waited ${seconds} seconds`
}

我该怎么做?

I have some code that loops through an array of promises, and outputs the value.

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

for (var promise of promises) {
  promise.then(seconds => console.log(`waited ${seconds} seconds`));
}

The issue with this is that the promise results don't get logged in the order of the array. My expected result is this:

Waited 1 seconds
Waited 3 seconds
Waited 2 seconds
Waited 4 seconds
Waited 5 seconds

And the result was this:

Waited 1 seconds
Waited 2 seconds
Waited 3 seconds
Waited 4 seconds
Waited 5 seconds

So I would like to have something like this

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

for (var promise of promises) {
  // When the promise is resolved, log `Waited ${seconds} seconds`
}

How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神也荒唐 2025-02-20 10:13:37

如果您不希望计时器一次开始全部启动,则不应一次进行所有这些等待一次调用。相反,只有在上一个产生的承诺得到解决时,只有下一个电话:

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const tasks = [
  () => wait(1),
  () => wait(3),
  () => wait(2),
  () => wait(4),
  () => wait(5),
];

(async function () {
  for (const task of tasks) {
    const seconds = await task();
    console.log(`waited ${seconds} seconds more`);
  }
})();

如果您 do 想要同时开始所有承诺,并且想要等待它们的全部在开始输出结果之前解决,然后使用Promise> Promise .all

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

Promise.all(promises).then(values => {
  console.log("all results are in:");
  for (const seconds of values) {
    console.log(`waited ${seconds} seconds (from the start)`);
  }
});

如果您 do 希望所有承诺要同时开始,并且想等待它们仅在上一个解决方案后才解决,请使用等待

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

(async function () {
  for (const promise of promises) {
    const seconds = await promise;
    console.log(`waited ${seconds} seconds (from the start)`);
  }
})();

If you don't want the timers to start all at once, you shouldn't make all those wait calls at once. Instead only make the next call when the previous one's resulting promise has resolved:

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const tasks = [
  () => wait(1),
  () => wait(3),
  () => wait(2),
  () => wait(4),
  () => wait(5),
];

(async function () {
  for (const task of tasks) {
    const seconds = await task();
    console.log(`waited ${seconds} seconds more`);
  }
})();

If you do want all promises to start at the same time, and want to wait for all of them to resolve before starting to output the results, then use Promise.all:

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

Promise.all(promises).then(values => {
  console.log("all results are in:");
  for (const seconds of values) {
    console.log(`waited ${seconds} seconds (from the start)`);
  }
});

If you do want all promises to start at the same time, and want to wait for them to resolve only after the previous one resolved, then use await:

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [
  wait(1),
  wait(3),
  wait(2),
  wait(4),
  wait(5),
];

(async function () {
  for (const promise of promises) {
    const seconds = await promise;
    console.log(`waited ${seconds} seconds (from the start)`);
  }
})();

肥爪爪 2025-02-20 10:13:37

丹尼尔·怀特(Daniel A.

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [wait(1), wait(3), wait(2), wait(4), wait(5)];

Promise.all(promises).then((results) => {
  for (var seconds of results) {
    console.log(`waited ${seconds} seconds`);
  }
});

Daniel A. White is right - use Promise.all() - something like this:

function wait(seconds) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(seconds);
    }, seconds * 1000);
  });
}

const promises = [wait(1), wait(3), wait(2), wait(4), wait(5)];

Promise.all(promises).then((results) => {
  for (var seconds of results) {
    console.log(`waited ${seconds} seconds`);
  }
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文