21

It seems I'm unable to use an async function as the first argument to Array.find(). I can't see why this code would not work what is happening under the hood?


function returnsPromise() {
  return new Promise(resolve => resolve("done"));
}

async function findThing() {
  const promiseReturn = await returnsPromise();
  return promiseReturn;
}

async function run() {
  const arr = [1, 2];
  const found = await arr.find(async thing => {
    const ret = await findThing();
    console.log("runs once", thing);
    return false;
  });
  console.log("doesn't wait");
}

run();

https://codesandbox.io/s/zk8ny3ol03

Laurie Poulter
  • 424
  • 2
  • 4
  • 10

4 Answers4

24

Simply put, find does not expect a promise to be returned, because it is not intended for asynchronous things. It loops through the array until one of the elements results in a truthy value being returned. An object, including a promise object, is truthy, and so the find stops on the first element.

If you want an asynchronous equivalent of find, you'll need to write it yourself. One consideration you'll want to have is whether you want to run things in parallel, or if you want to run them sequentially, blocking before you move on to the next index.

For example, here's a version that runs them all in parallel, and then once the promises are all resolved, it finds the first that yielded a truthy value.

async function findAsync(arr, asyncCallback) {
  const promises = arr.map(asyncCallback);
  const results = await Promise.all(promises);
  const index = results.findIndex(result => result);
  return arr[index];
}

//... to be used like:

findAsync(arr, async (thing) => {
  const ret = await findThing();
  return false;
})
Nicholas Tower
  • 56,346
  • 6
  • 64
  • 75
  • Ok that's what I suspected. I googled a lot and couldn't find anything about this anywhere. Is it documented that find works with async functions like this? – Laurie Poulter Apr 09 '19 at 20:48
  • 1
    It's documented that `.find` loops through the array until an element returns a truthy value, and it's documented that async functions return promises. This is just the interaction of those two things. – Nicholas Tower Apr 09 '19 at 20:49
  • @LauriePoulter nothing in Array.protoype is asynchronous – charlietfl Apr 09 '19 at 20:50
  • `Promise.all` will wait for every promise to resolve. It would be nice to wait until a promise resolves truthy. – Gherman Dec 21 '19 at 14:55
  • 1
    Might be helpful to use `Promise.race`, which would help finish this quicker than `Promise.all` if you're looking for a singular match. – Jacob Morrison Jan 29 '20 at 17:45
  • @JacobMorrison `Promise.race` will finish quicker, but could finish without a match. – Roberto Damian Alfonso May 12 '20 at 21:46
10

Here is a TypeScript version that runs sequentially:

async function findAsyncSequential<T>(
  array: T[],
  predicate: (t: T) => Promise<boolean>,
): Promise<T | undefined> {
  for (const t of array) {
    if (await predicate(t)) {
      return t;
    }
  }
  return undefined;
}
Sebastien Lorber
  • 84,553
  • 62
  • 274
  • 401
0

It might help you to note that Array.prototype.filter is synchronous so it doesn't support async behaviour. I think that the same applies to the "find" property. You can always define your own async property :) Hope this helps!

0

The other answers provide two solutions to the problem:

  1. Running the promises in parallel and awaiting all before returning the index
  2. Running the promises sequencially, so awaiting every single promise before moving on to the next one.

Imagine you have five promises that finish at different times: The first after one second, the second after two seconds, etc... and the fifth after five seconds.

If I'm looking for the one that finished after three seconds:

  1. The first solution will wait 5 seconds, until all promises are resolved. Then it looks for the one that matches.
  2. The second one will evaluate the first three matches (1 + 2 + 3 = 6 seconds), before returning.

Here's a third option that should usually be faster: Running the promises in parallel, but only waiting until the first match ("racing them"): 3 seconds.

function asyncFind(array, findFunction) {
  return new Promise(resolve => {
    let i = 0;
    array.forEach(async item => {
      if (await findFunction(await item)) {
        resolve(item);
        return;
      }
      i++;
      if (array.length == i) {
        resolve(undefined);
      }
    });
  });
}

//can be used either when the array contains promises
var arr = [asyncFunction(), asyncFunction2()];
await asyncFind(arr, item => item == 3);

//or when the find function is async (or both)
var arr = [1, 2, 3];
await asyncFind(arr, async item => {
    return await doSomething(item);
}

When looking for a non-existant item, solutions 1 and 3 will take the same amount of time (until all promises are evaluated, here 5 seconds). The sequencial approach (solution 2) would take 1+2+3+4+5 = 15 seconds.

Demo: https://jsfiddle.net/Bjoeni/w4ayh0bp

Bjoeni
  • 1