gpt4 book ai didi

javascript - 节点等待不等待 promise

转载 作者:行者123 更新时间:2023-11-30 19:22:30 25 4
gpt4 key购买 nike

我有一个 promise ,我正在尝试等待,但它不起作用。

这是我的方法:

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;
}

我是这样调用它的:

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

我的输出是:

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

Promise Promise { }

但是,该方法立即返回,await promise 不会等待 promise 被解析。

为什么不呢?

我调用的方法也是异步的:

export const getSearchResults = async (env: Environment, searchConfig: SearchConfig, query: string): Promise<ISearchResult> => {
return new Promise((resolve, reject) => {
... resolve/reject as appropriate ...
});
};

最佳答案

还有一些其他问题。我模拟了您的代码,等待工作正常。

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);

关于javascript - 节点等待不等待 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com