Arrow functions, this and that, in V8

154 views
Skip to first unread message

Bruce Mcpherson

unread,
Mar 10, 2020, 7:36:47 AM3/10/20
to Google Apps Script Community
Arrow functions can help you create nice code with less clutter, but they are not just syntactic sugar. As you are moving over to V8, here's an intro into what's changed to help.

Tanaike

unread,
Mar 11, 2020, 2:58:35 AM3/11/20
to Google Apps Script Community
I think that it is a good information. I would like to study from it. Thank you so much.

Andrew Apell

unread,
Mar 11, 2020, 6:14:09 AM3/11/20
to Google Apps Script Community
Thanks Bruce,
I was once firmly in the "syntactic sugar" camp but posts like these are moving me away... slowly.

BTW, is there any issue with JSON and the new V8 runtime? Or does it work the same as it was in Rhino?

Bruce Mcpherson

unread,
Mar 11, 2020, 10:28:20 AM3/11/20
to Google Apps Script Community
Can't think of any json implications - do you have a specific concern?

Andrew Apell

unread,
Mar 11, 2020, 2:25:10 PM3/11/20
to Google Apps Script Community
Just a hunch but my authentication flow, which depends on JSON (the only "prominent" line of code in my work), was failing until I switched back to Rhino.
Of course, I need to do more investigating before I can isolate the issue.

Andrew Apell

unread,
Mar 13, 2020, 8:37:53 AM3/13/20
to google-apps-sc...@googlegroups.com
Hello Bruce,
In V8, do I need to use Promises when using UrlFetchApp?

Bruce Mcpherson

unread,
Mar 13, 2020, 9:52:30 AM3/13/20
to google-apps-sc...@googlegroups.com
No
In fact Apps Script is Synchronous, so Promises are only for compatibility so you can perhaps share the same code between client and server side. UrlFetch doesn't return a Promise, so you'd anyway need to construct one. 

These two calls are equivalent

function myFunction() {
  const result = UrlFetchApp.fetch(url)
  Logger.log(result)

  const p = new Promise ((resolvereject) => {
    const result = UrlFetchApp.fetch(url)
    resolve(result)
  })

  p.then(result=> {
    Logger.log(result)
  })
}

On Fri, 13 Mar 2020 at 12:37, Andrew Apell <chis...@gmail.com> wrote:
Hello Bruce,
In V8, do I need to use Promises when using Urlfetch?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/0cd31c6a-c879-44d7-b56f-bd252ac2340c%40googlegroups.com.

Andrew Apell

unread,
Mar 13, 2020, 9:57:08 AM3/13/20
to Google Apps Script Community
👍

Thanks. That saves me from rewriting many lines of code... been dodging promises for a long time :)
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

Bruce Mcpherson

unread,
Mar 13, 2020, 10:02:56 AM3/13/20
to google-apps-sc...@googlegroups.com
btw - the async/await syntax is also supported, but of course does nothing since apps script is synchronous.

  (async (url) => {
    const result = await new Promise ((resolvereject) => {
      const result = UrlFetchApp.fetch(url)
      resolve(result)
    })

👍

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/40914d86-45dd-4b2e-8699-6eea963703a3%40googlegroups.com.

Alex

unread,
Mar 16, 2020, 5:18:35 AM3/16/20
to Google Apps Script Community
At some point, I believed in something else =) I wanted a little parallelism.

Thank you Bruce!
This is great content!
Reply all
Reply to author
Forward
0 new messages