Madelene Sköld
09/16/2022, 6:53 AMasync function execute(scriptContext) {
let transactionSearch = search.load('searchinternalid')
let pendingPromise = runSearch(transactionSearch);
log.debug('execute', 'Meanwhile');
let myResults = await pendingPromise;
log.debug('execute', 'myResults: ' + JSON.stringify(myResults))
}
function runSearch(transactionSearch) {
log.debug('runSearch', 'Start of runSearch');
let searchResults = [];
return new Promise(resolve => {
transactionSearch.run().each(result => {
searchResults.push(result.id);
return true;
});
log.debug('runSearch', 'Finishing runSearch');
resolve(searchResults);
})
}
battk
09/16/2022, 7:10 AMbattk
09/16/2022, 7:10 AMbattk
09/16/2022, 7:11 AMbattk
09/16/2022, 7:13 AMbattk
09/16/2022, 7:17 AMbattk
09/16/2022, 7:17 AMbattk
09/16/2022, 7:18 AMMadelene Sköld
09/16/2022, 7:21 AMMadelene Sköld
09/16/2022, 7:24 AMbattk
09/16/2022, 7:24 AMbattk
09/16/2022, 7:25 AMMadelene Sköld
09/16/2022, 7:25 AMthe Promise you wrote is trying to wait on itself
This is where I probably don’t fully understand how promises work. I’ve been spending quite some time reading up on them but I’m clearly still missing the basic understanding hahabattk
09/16/2022, 7:26 AMbattk
09/16/2022, 7:27 AMMadelene Sköld
09/16/2022, 7:27 AMbattk
09/16/2022, 7:27 AMbattk
09/16/2022, 7:28 AMbattk
09/16/2022, 7:28 AMbattk
09/16/2022, 7:28 AMMadelene Sköld
09/16/2022, 7:29 AMMadelene Sköld
09/16/2022, 7:31 AMbattk
09/16/2022, 7:38 AMMadelene Sköld
09/16/2022, 7:56 AMbattk
09/16/2022, 8:02 AMMadelene Sköld
09/16/2022, 8:25 AMasync function execute(scriptContext) {
startTime = Date.now();
log.debug('execute', getElapsedTime() + ': START execute');
let transactionSearch = search.create({
type: search.Type.TRANSACTION,
columns: ['tranid', 'entity', 'trandate', 'item', 'type']
});
let promise;
try {
promise = runSearch(transactionSearch);
} catch (e) {
log.error('execute', getElapsedTime() + ': catch 1 error: ' + JSON.stringify(e))
}
log.debug('test', getElapsedTime() + ': meanwhile.....');
let results;
try {
results = await promise;
log.debug('test', getElapsedTime() + ': results: ' + results.length);
} catch (e) {
log.error('execute', getElapsedTime() + ': catch 2 error: ' + JSON.stringify(e))
}
}
function getElapsedTime() {
return Date.now() - startTime
}
function runSearch(search) {
log.debug('test', getElapsedTime() + ': starting to run search')
let searchResults = [];
return new Promise(resolve => {
let counter = 0;
search.run().each.promise(result => {
counter++;
let lineObj = {
internalid: result.id,
tranid: result.getValue('tranid'),
trandate: result.getValue('trandate')
}
searchResults.push(lineObj);
if (searchResults.length === 999) log.debug('test', getElapsedTime() + ': 999 results')
return true;
});
log.debug('test', getElapsedTime() + ': finishing run search');
resolve(searchResults);
});
}
Madelene Sköld
09/16/2022, 8:26 AMMadelene Sköld
09/16/2022, 8:29 AMbattk
09/16/2022, 8:33 AMtry {
promise = runSearch(transactionSearch);
} catch (e) {
log.error('execute', getElapsedTime() + ': catch 1 error: ' + JSON.stringify(e))
}
battk
09/16/2022, 8:34 AMbattk
09/16/2022, 8:36 AMbattk
09/16/2022, 8:37 AMreturn new Promise(resolve => {
let counter = 0;
search.run().each.promise(result => {
counter++;
let lineObj = {
internalid: result.id,
tranid: result.getValue('tranid'),
trandate: result.getValue('trandate')
}
searchResults.push(lineObj);
if (searchResults.length === 999) log.debug('test', getElapsedTime() + ': 999 results')
return true;
});
log.debug('test', getElapsedTime() + ': finishing run search');
resolve(searchResults);
});
battk
09/16/2022, 8:38 AMbattk
09/16/2022, 8:46 AMbattk
09/16/2022, 8:46 AMMadelene Sköld
09/16/2022, 8:46 AMbattk
09/16/2022, 8:48 AMMadelene Sköld
09/16/2022, 8:48 AMMadelene Sköld
09/16/2022, 8:48 AMbattk
09/16/2022, 8:49 AMbattk
09/16/2022, 8:50 AMbattk
09/16/2022, 8:50 AMbattk
09/16/2022, 8:53 AMbattk
09/16/2022, 8:54 AMfinishing run search
to tell when your search is donebattk
09/16/2022, 8:54 AMbattk
09/16/2022, 8:54 AMMadelene Sköld
09/16/2022, 9:02 AMMadelene Sköld
09/16/2022, 9:02 AMMadelene Sköld
09/16/2022, 9:09 AMbattk
09/16/2022, 9:09 AMbattk
09/16/2022, 9:09 AMMadelene Sköld
09/16/2022, 9:18 AMbattk
09/16/2022, 9:22 AMWatz
09/17/2022, 8:47 AMbattk
09/17/2022, 11:57 AMquery.runSuiteQL().asMappedResults()
is representative of your attempts, then you are making the same mistake as @Madelene Sköldbattk
09/17/2022, 11:57 AMbattk
09/17/2022, 11:58 AMbattk
09/17/2022, 12:09 PMbattk
09/17/2022, 12:11 PMWatz
09/17/2022, 12:15 PMquery.runSuiteQL().asMappedResults()
in a promise. But as you say, only the api's that have a promise version actually works to run asynchronously. And runSuiteQL doesn't.Watz
09/17/2022, 12:32 PMrequire(['N/query'], (query) => {
function execSuiteQLAsync(SuiteQL) {
return new Promise((resolve, reject) => {
let results
try {
results = query.runSuiteQL({
query: SuiteQL
}).asMappedResults()
} catch(e) {
reject('error')
}
resolve(results)
})
}
const runSuiteQL = async function() {
log.debug('start of async function')
let data = await Promise.all([
execSuiteQLAsync(`SELECT id FROM transaction WHERE id between 0 and 2000`),
execSuiteQLAsync(`SELECT id FROM transaction WHERE id between 2000 and 4000`)
])
return data
}
console.log('start')
runSuiteQL()
.then(data =>{
log.debug('data',data)
})
.catch(e=>log.debug('error',e))
})
battk
09/17/2022, 12:36 PMbattk
09/17/2022, 12:37 PMWatz
09/17/2022, 12:37 PMWatz
09/17/2022, 12:41 PMawait Promise.all()
was the bit that did the parallel execution part. If we assume that execSuiteQLAsync is some async function that actually behaves as such, what is making it synchronous?battk
09/17/2022, 12:42 PMbattk
09/17/2022, 12:43 PMbattk
09/17/2022, 12:47 PMWatz
09/17/2022, 12:47 PMWatz
09/17/2022, 12:48 PMbattk
09/17/2022, 12:49 PMbattk
09/17/2022, 12:49 PMWatz
09/17/2022, 12:53 PMbattk
09/17/2022, 12:53 PMWatz
09/17/2022, 12:57 PMbattk
09/17/2022, 1:03 PMWatz
09/17/2022, 1:03 PMWatz
09/17/2022, 1:03 PMbattk
09/17/2022, 1:03 PMWatz
09/17/2022, 1:04 PMbattk
09/17/2022, 1:04 PMWatz
09/17/2022, 1:06 PMWatz
09/17/2022, 1:06 PMbattk
09/17/2022, 1:07 PMWatz
09/17/2022, 1:08 PMbattk
09/17/2022, 1:08 PMbattk
09/17/2022, 1:08 PMbattk
09/17/2022, 1:10 PMWatz
09/17/2022, 1:10 PMbattk
09/17/2022, 1:11 PMWatz
09/17/2022, 1:11 PMbattk
09/17/2022, 1:11 PMWatz
09/17/2022, 1:18 PMWatz
09/17/2022, 1:20 PMWatz
09/17/2022, 1:29 PMMadelene Sköld
09/18/2022, 1:38 PMMadelene Sköld
09/18/2022, 1:41 PMWatz
09/18/2022, 2:39 PMMadelene Sköld
09/21/2022, 6:24 AM