🍊

Promise in Google Apps Script

2020/11/22に公開

summary

  • logged as if await considered.
  • execution time can't be shortend(duration counts evaluated as if ran synchronously).

await

const pt = async () => {
    const p1 = new Promise((resolve, reject) => {
        Utilities.sleep(2000);
        resolve("one");
    });
    const p2 = new Promise((resolve, reject) => {
        Utilities.sleep(5000);
        resolve("two");
    });
    const t1 = new Date();
    console.log(">");
    await Promise.all([p1, p2]).then(results => {
        console.log(results);
    }).catch(reject => {
        console.log(reject);
    });
    console.log("<");
    console.log((new Date()) - t1);
}
Head	pt	エディタ	2020/11/22 22:49:32	7.399 秒	
完了
2020/11/22 22:49:39	デバッグ	>
2020/11/22 22:49:39	デバッグ	[ 'one', 'two' ]
2020/11/22 22:49:39	デバッグ	<
2020/11/22 22:49:39	デバッグ	23

no await

const pt = async () => {
    const p1 = new Promise((resolve, reject) => {
        Utilities.sleep(2000);
        resolve("one");
    });
    const p2 = new Promise((resolve, reject) => {
        Utilities.sleep(5000);
        resolve("two");
    });
    const t1 = new Date();
    console.log(">");
    await Promise.all([p1, p2]).then(results => {
        console.log(results);
    }).catch(reject => {
        console.log(reject);
    });
    console.log("<");
    console.log((new Date()) - t1);
}
Head	pt	エディタ	2020/11/22 22:47:24	7.37 秒	
完了
2020/11/22 22:47:31	デバッグ	>
2020/11/22 22:47:31	デバッグ	<
2020/11/22 22:47:31	デバッグ	6
2020/11/22 22:47:31	デバッグ	[ 'one', 'two' ]

Discussion