简体   繁体   English

在脚本标签中使用 Promise 不起作用?

[英]Using Promises in script tag not working?

My below code is used to append scripts in DOM using promises.我下面的代码用于使用承诺在 DOM 中附加脚本。 (All below code executes inside iframe for A-frame technology and my code is being generated using Google Blockly[Block-based coding] ) (以下所有代码都在 iframe 内执行,用于 A 帧技术,我的代码是使用 Google Blockly[Block-based coding] 生成的)

 export const appendScript = async({ src, aframeWindow, code, id }, doc, callback, ) => new Promise(resolve => { const startDate = new Date(); const script = doc.createElement('script'); if (src) script.src = src; if (code) { script.text = code; } if (id) { script.id = id; } doc.head.appendChild(script); if (callback) callback(); if (code) { if (callback) { callback(); } console.log(aframeWindow.onGreenPlayButtonClicked, "===="); // undefined when code string contains await outside ongreenplaybuttonclicked fn resolve('resolved'); } else { script.addEventListener('load', () => { const endDate = new Date(); const seconds = (endDate.getTime() - startDate.getTime()) / 1000; console.log('script loaded: ', src, `took ${seconds} to load`); resolve('resolve2'); }); } });

The below script is my function that calls appendscript.下面的脚本是我调用 appendscript 的函数。

 const callAppendScript = async code => { const CODE_VERSION = aframeWindow.CODE_VERSION; code = ` async function main(inPlayMode) { let CURRENT_CODE_VERSION = CODE_VERSION; ${code}; } main(); `; let x = await appendScript({ code, aframeWindow, id: 'code' }, aframeWindow.document, ); } callAppendScript(code);

My code string 1 that generates : (donot work)我的代码字符串 1 生成:(不起作用)

 async function main(inPlayMode) { let CURRENT_CODE_VERSION = !inPlayMode ? 0 : window.CODE_VERSION; var greeting; greeting = await HatchTranslateEngine.translate(`Hello`, 'en'); //async window.onGreenPlayButtonClicked = async function onGreenPlayButtonClicked() { await MinecraftAvatar.speakSound(greeting, true) }; } main();

My code string 2 that generates : (works)我的代码字符串 2 生成:(有效)

 async function main(inPlayMode) { let CURRENT_CODE_VERSION = !inPlayMode ? 0 : window.CODE_VERSION; var greeting; greeting = Hatch.convert(`Hello`, 'jp'); //syncrhonous window.onGreenPlayButtonClicked = async function onGreenPlayButtonClicked() { await MinecraftAvatar.speakSound(greeting, true) }; } main();

My code string 3 that generates: (still works)我生成的代码字符串 3:(仍然有效)

 async function main(inPlayMode) { let CURRENT_CODE_VERSION = !inPlayMode ? 0 : window.CODE_VERSION; var greeting; window.onGreenPlayButtonClicked = async function onGreenPlayButtonClicked() { greeting = await HatchTranslateEngine.translate(`Hello`, 'en'); //async this still works (inside playbtn) await MinecraftAvatar.speakSound(greeting, true) }; } main();

Whenever i click play button, callAppendScript(code) gets executed , and when it does so,每当我点击播放按钮时, callAppendScript(code)就会被执行,当它这样做时,

For code string 1 , it gives me undefined in aframeWindow.onGreenPlayButtonClicked (incorrect) For code string 2 , it gives me function signature of onGreenPlayButtonClicked in aframeWindow.onGreenPlayButtonClicked (correct)对于代码字符串 1 ,它在aframeWindow.onGreenPlayButtonClicked给了我未定义(不正确)对于代码字符串 2 ,它给了我在 aframeWindow.onGreenPlayButtonClicked中的aframeWindow.onGreenPlayButtonClicked函数签名(正确)

So, why is it not working for case 1 , I tried everything but could not figure it out.那么,为什么它不适用于 case 1 ,我尝试了所有方法但无法弄清楚。 I only found out that for any await keyword that is outside onGreenPlayButtonClicked, I get aframeWindow.onGreenPlayButtonClicked which is inside appendScript function.我只发现对于 onGreenPlayButtonClicked 之外的任何await关键字,我都会得到aframeWindow.onGreenPlayButtonClicked ,它在appendScript函数内。

Thankyou谢谢

Nothing in the appendScript waits for the promise that the main() call returns. appendScript没有任何appendScript等待main()调用返回的承诺。 If you are asynchronously initialising onGreenPlayButtonClicked , it won't be immediately available after appending the script element.如果您异步初始化onGreenPlayButtonClicked ,则在附加脚本元素后它不会立即可用。

I would suggest you try我建议你试试

const callAppendScript = async code => {
  const CODE_VERSION = aframeWindow.CODE_VERSION;
  code = `
    async function main(inPlayMode) {
      let CURRENT_CODE_VERSION = CODE_VERSION;
      ${code};
    }
    // no main() call here
  `;
  await appendScript({
    code,
    aframeWindow,
    id: 'code'
  }, aframeWindow.document);

  console.log(aframeWindow.onGreenPlayButtonClicked, "before");
  await main();
//^^^^^^^^^^^^
  console.log(aframeWindow.onGreenPlayButtonClicked, "after");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM