最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use Array.prototype.map() with then() for connection.end() - Stack Overflow

programmeradmin0浏览0评论

I have an error TypeError: cars.map(...).then is not a function. I want terminate connection when update the color.

cars.map(function (item, i) {
    if (item.color === 'red') {
        updateColor(item.id)
    }
}).then(() => connection.end())

I have an error TypeError: cars.map(...).then is not a function. I want terminate connection when update the color.

cars.map(function (item, i) {
    if (item.color === 'red') {
        updateColor(item.id)
    }
}).then(() => connection.end())
Share Improve this question asked Oct 10, 2019 at 8:45 user10408704user10408704 3
  • map works with Array only. Did you initialize cars as an array? again then also not work with map – Harish Commented Oct 10, 2019 at 8:49
  • You are chaining then() to map(), you just can chain then() to a function that returns a promise – Oriol Grau Commented Oct 10, 2019 at 8:50
  • 1 I'm not sure how these two are connected. If you have a cars array already, then you don't need a connection, so you can simply terminate before the .map call and nothing should change. Or you can call cars.map(/* ... */); connection.end(); which is guaranteed to run after the mapping operation. – VLAZ Commented Oct 10, 2019 at 8:53
Add a comment  | 

2 Answers 2

Reset to default 11

This answer only applies in case updateColor is asynchronous and indeed returns a Promise object.

.map returns an array of something, not a Promise. What you can do, is building an array of Promise using .map and then resolve them all using Promise.all and then deal with the result.

The following code will execute all updateColor, wait until they are done and then stop the connection.

Promise.all(cars.map((item, i) => {
    if (item.color === 'red') {
      return updateColor(item.id);
    }

    return false;
  }))
  .then(() => connection.end())

Using async/await instead of Promises

  await Promise.all(cars.map((item) => {
        if (item.color === 'red') {
          return updateColor(item.id);
        }

        return false;
      }));

   connection.end();

using reduce instead of map

await Promise.all(cars.reduce((tmp, x) => (x.color === 'red' ? [
  ...tmp,

  updateColor(x.id),
] : tmp), []));

connection.end();

There are a couple of issues there:

  1. There's no reason to use map when you aren't using the array it creates. To loop through the array, use forEach or any of several other mechanisms.

  2. map is a fully synchronous function. To do something after it, just...do the thing after it.

  3. then is a method of promises (well, thenable). map returns an array, not a promise.

So:

for (const item of cars) {
    if (item.color === 'red') {
        updateColor(item.id)
    }
};
connection.end();

If updateColor does its work asynchronously and returns a promise, then see Grégory NEUT's answer.

发布评论

评论列表(0)

  1. 暂无评论