So, what would happen if I used the ```record.crea...
# suitescript
d
So, what would happen if I used the
Copy code
record.create.promise
in a restlet? Meaning .. I want to capture the incoming payload in a custom record, but also want to return a 200 as a response as quickly as possible (since the status of the operation isn't needed in the response)
s
I haven't tried it but I think restlets are synchronous - give it a try and report back?
g
meaning that you want a response written but continue to process? You can definitely do this. Promises or not. You won’t be able to respond again and update your response ( w/o some other magic not shown here)
🤔 1
d
Agreed.
I intend to process the newly created record (holding the restlet payload) later in a Map/Reduce.
c
If it doesn't matter could you do the reverse and have NetSuite just poll for whatever it needs?
d
One way @creece
I'll throw some logs in and report back
c
If you're just creating a custom record and returning I can't imagine thats slow enough to create something that'll be quicker
Might be worth knowing that your process at least made it through successfully. Otherwise you may want to retry or something which warrants the waiting for the custom record to be created.
d
all true statements @creece... just pushing envelopes I guess
Ha! So .. here's my findings
In short, I responded to the webhook BEFORE my promise had completed. That's really all I wanted to do/know
Opens up a few ideas on how to do things in restlets/suitelets
s
actually, the important this is whether your promise code did complete even though the response was returned?
I think you should log something in
.then
and see if it ever makes it to the execution log
So log
about to return from the restlet
, return your JSON response, then log something in that promise handler (
made it to the handler
) If you don't get both messages then you know that promise handler was never called?
This chart clearly states that any
record.
promises are not supported server-side (RESTlet). ... so @Shawn Talbert, you're right! (And I'm sad)
s
don't be sad! The most common workaround I see here is folks doing a rudimentary queue mechanism where the restlet only saves the input to a custom record, then immediately returns. A scheduled script or similar comes along later to process the data async
I don't like using NS as a queue this way given that there are far more robust and purpose-built queuing mechanisms out there, but I see it done. Seems this is often by folks that don't have experience outside NS and/or constraints that require the solution to be solely contained inside NS.
d
What's interesting though is I was able write the
promise
and not only did it run, but ran successfully.
So, an argument could be made to use the .promise style of coding. It just may not always run asynchronously.
Plus, when that module does become asynchronous, viola ... you've already coded for it!
(I'm on of those guys that builds a queue using M/R ... because my solutions need to be 100% NS)
s
I thought you confirmed the promise did not work as expected? Simply creating a promise is rather useless compared to doing something when the promise resolves
In my experience, NS is a crummy queue, and I have yet to see anyone implement either basic functionality that's available in other queueing solutions or even automatic deleting of queue records (culling)
I sympathize with your plight
@darrenhillconsulting, did you confirm that the
.then()
on your promise never gets invoked sever-side?
d
I'm still experimenting
g
it’s not that you can’t run promises at all… it’s that there is no benefit to doing so for
N/record
and no api…..YET. Behind the scenes (outside the JS REPL) an operation like reading a record from the database might benefit from offloading that via threading but it has to be implemented that way. NetSuite doesn’t tell us where these optimizations have been made but there are clues such as promises being available in the api. So it seems to have been done for search and query and web requests. If you really wanted it to be a promise /async structure you could wrap it in a promise. c.p. there’s no performance benefit to it on the server-side. let’s say they were planning on having internal optimization for N/record. Then making a promise method available would make sense. You could write code today that does not perform better but logically expects it to be asynchronous. When it becomes asynchronous then you get the benefit automatically without a re-write. in short, Today you could stick
record.create
into a promise simply with
Promise.resolve(record.create({options}))
hypothetically if record.create.promise becomes available on the server side you can rewrite that one line to take advantage and logically everything would still flow. if you’re feeling optimistic or have some insider knowledge, you could have your stub or proxy for it. Logically equivalent to this ugliness:
Copy code
Promise.resolve((create=>create({options}))(record.create.promise || record.create)).then(/*etc*/)
i’m not suggesting you should do this — simply trying to keep the neurons firing.
s
I definitely wouldn't do any of the above.