6

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())
  • `map` works with Array only. Did you initialize `cars` as an array? again `then` also not work with `map` – Harish Oct 10 '19 at 08:49
  • You are chaining `then()` to `map()`, you just can chain `then()` to a function that returns a promise – Oriol Grau Oct 10 '19 at 08: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 Oct 10 '19 at 08:53

2 Answers2

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();
vizsatiz
  • 1,933
  • 1
  • 17
  • 36
Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
4

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875