我以为我明白诺言,直到这发生了。我知道在SO上有很多类似的问题,但是经过许多次重做后,这个简单的问题仍未解决。我一直在阅读和尝试,但是如果不使用回调函数并且不使用.then
,就无法找到解决方法。const searchResponse = await performSearch(options);
if (searchResponse.error) {
response.error = searchResponse.error;
}
else {
response = orchestrateShareClassSearchResponse(searchResponse['data']);
}
return Promise.resolve(response);
问题是searchResponse ['data']始终作为空对象发送,并且不填充performSearch(异步功能performSearch)的结果。我也尝试过等待searchResponse ['data'],但无济于事。
有时候,微小的东西确实会造成问题。除了使用回调,还有人有其他想法吗?
我也发布了performSearch,尽管并不想使问题变得复杂:
export async function performSearch(options, callingMethod: string, callback?: Function): Promise<IActionResponse<ISearchEntityResponse>> {
const baseSearchRequest = Object.assign({}, BASE_SEARCH_REQUEST, options);
let response = <IActionResponse<ISearchEntityResponse>>{};
let searchResponse;// = await performSearch(options);
const io = require('socket.io-client');
const socket = io.connect();
socket.on('connect', function (data) {
console.log('CONNECTED TO CLIENT SOCKET');
socket.emit('fetchShareClasses', null);
socket.on('fetchShareClasses', async function (searchResponse) {
console.log('sNullOrUndefined(searchResponse)::', isNullOrUndefined(searchResponse));
if (searchResponse === null) {
searchResponse = await fetch(ENDPOINTS.fund.searchEntity, generateRequest('POST', baseSearchRequest)).then(function (searchResponse) {
console.log('NULL recieved do call to get DATA');
socket.emit('fetchShareClasses', searchResponse);
});
}
else {
console.log('fetchShareClasses CLIENT REACHED');
response = searchResponse;
}
});
});
return Promise.resolve(response);
}