Can async/await be used with script 2.1? I thought...
# suitescript
s
Can async/await be used with script 2.1? I thought it could be, but it still seems to run in sync mode.
Copy code
let promise = new Promise((resolve) => {
    log.debug('perris items');
    const norcrossItems = new seHelper.Search({
        searchId: 'customsearch_mhi_ad_expected_nxt_po_da_2',
        filters: ['location', 'anyof', '8']
    }).toList();
    resolve(norcrossItems);
});
let promise2 = new Promise((resolve) => {
    log.debug('norcross items');
    const norcrossItems = new seHelper.Search({
        searchId: 'customsearch_mhi_ad_expected_nxt_po_da_2',
        filters: ['location', 'anyof', '9']
    }).toList();
    resolve(norcrossItems);
});
log.debug('2');
//promise.then(log.debug); //No longer using this
let responseBody = await promise; //Using await instead
let responseBody2 = await promise2; //Using await instead
This is logging perris items, norcross items, 2. When it should be 2, perris items, norcross items
b
Async await can be used but it still runs synchronously
s
Well, poops! 💩
I have 3 searches that need to be merged and wanted to run all 3 searches at the same time, and then do my fancy smancy merge at the end.
s
if performance was your goal, could you combine those searches into a single SuiteQL search?
s
I'm unfamiliar with this. Is this a place I can write sql? I know of the query module, but had never used it. Is that the same thing?
g
@screnshaw Suiteanswers 91329 should help you out with this.
❤️ 1
b
you would need to use one of the promised based methods like PagedData.fetch.promise if you wanted it to work async
your code doesnt do anything that would put the code back onto the microtask queue
s
Whatcya mean battk?
b
Copy code
let promise = new Promise((resolve) => {
    log.debug('perris items');
    const norcrossItems = new seHelper.Search({
        searchId: 'customsearch_mhi_ad_expected_nxt_po_da_2',
        filters: ['location', 'anyof', '8']
    }).toList();
    resolve(norcrossItems);
});
nothing inside that promise is asyncronous
s
It may be my hope, but I was hoping is it would trigger the code, and then once you get to the await promise, it would then wait for the results to finish
b
promises dont branch the code inside it into a different process
Promises become asyncronous when something inside it does something that waits on a result
s
Ah. Is there a way with netsuite to do what I'm asking then?
b
use one of the promise based methods
that returns a Promise, like the one I linked earlier
s
Lame. was hoping I could do like in angular and make them async calls