1

I have a promise that I'm trying to await but it is not working.

Here's my method:

async function lookupAndAddManager(id, record, fieldNameToAdd) {
    console.log(`Searching ${id}`);
    let promise = ups.getSearchResults(ups.Environment.Prod, ups.SearchConfig.Email, ups.emailFromId(id))
    console.log("Promise", promise)
    let response = await promise;
}

I'm calling it like this:

lookupAndAddManager(row.BPO, row, 'BPO_MGR')

My output is:

Searching CN=Name/OU=TEST/O=EXAMPLE

Promise Promise { }

However, the method is returning right away, the await promise does not wait for the promise to be resolved.

Why not?

The method I'm calling is also async:

export const getSearchResults = async (env: Environment, searchConfig: SearchConfig, query: string): Promise<ISearchResult> => {
    return new Promise((resolve, reject) => {
        ... resolve/reject as appropriate ...
    });
};
mikeb
  • 10,578
  • 7
  • 62
  • 120
  • 1
    `await lookupAndAddManager(row.BPO, row, 'BPO_MGR')` You should call it like this as well. It returns a promise. – Rajaprabhu Aravindasamy Aug 01 '19 at 12:49
  • 3
    It's an async method, so, of course, it will return right away...unless you await lookupAndAddManager too in the function that calls it. That's how async works. – Wyck Aug 01 '19 at 12:49
  • 1
    just to confirm you are using await to call your async function lookupAndAddManager, right? – Saha K P Ghoshal Aug 01 '19 at 12:50
  • No, I'm calling `lookupAndAddManager` from my main entry point, so it's not being called from an async method. I have to, at some point, call the method from a non-async method – mikeb Aug 01 '19 at 12:54
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Igor Aug 01 '19 at 13:05
  • See the aforementioned [suggested duplicate](https://stackoverflow.com/q/14220321/1260204). Asynchronous calls are a common and critical building block in writing/designing an application. It is critical that you understand how to work with asynchronous calls in javascript. Understanding these core concepts will help you become a better programmer and also ensure you do not keep "stubbing your toe" on the same problem. – Igor Aug 01 '19 at 13:05
  • @mikeb on what basis you are saying it is not awaiting? and also there is a chance that you are not resolving promise properly from getSearchResults causing the probel. – Ganesh Karewad Aug 01 '19 at 13:37

1 Answers1

1

There is some other issue. I simulated your code and the await works fine.

async function lookupAndAddManager(id, record, fieldNameToAdd) {
    console.log(`Searching ${id}`);
    let promise = getSearchResults();
    console.log("Promise will be promised and then wait 3 seconds, now its just unresolved promise object", promise)
    let response = await promise;
    console.log("After 3 seconds, we got response", response);
}

const getSearchResults = async (env, searchConfig, query) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => resolve('Yeeey, resolved'), 3000);
    });
};

lookupAndAddManager(10);
libik
  • 22,239
  • 9
  • 44
  • 87