takeAsync not running within async .ts functions

Hi,

I’m trying to figure out whether this is an error in my async logic or a bug in the takeAsync Object API function.

If I make a function that uses takeAsync it doesn’t run if used within another async function for example:

public async fetchObjs(obj2, kValue): Promise<objs[]> {
   const vector = obj2.embedding
   const objs: objs[] = await Objects.search()
        .objs()
        .nearestNeighbors(obj => obj.vectorProperty.near(vector, { kValue }))
        .orderByRelevance()
        .takeAsync(kValue);
   console.log('search ran successfully')
   return objs
}

const newObjs = await this.fetchObjs(obj2, kvalue)

^^^ This works fine, however it doesn’t work with nothing in the logs and no error messages when ran within a map like this:

const obj2s: obj2[] = [a, b, c]

const obj1s: objs[][] = await Promise.all(obj2s.map(async o => await this.fetchObjs(o, kvalue)))

Any help at all on this would be greatly appreciated :slight_smile:

I’d assume your issue is that a promise isn’t properly being await-ed at some point. You can hover over types in the code repositories UI to help trace where promises are waited into real types.

I don’t think you need the async/await inside the map. You can just rely on the promise being passed through and then use the promise.all. This means you could just do await Promise.all(obj2s.map(o => this.fetchObjs(o, kvalue))), which should be a little easier to debug.

You can also try adding some logging right at the start of the fetchObjs method. You can also remove the await from fetchObjs and just return the promise from takeAsync.

1 Like

It was a missed await deep in the code, splitting it out into more functions helped discover it. Thanks for the help :slight_smile:

You can also set breakpoints in the Code Repositories UI when running a Function in Live Preview, and use them to step through your code. That may be useful debugging any future issues like this!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.