0

Currently I have the following code:

  let getThemeId = getMainThemeId();
  getThemeId.then(id => {
    themeId = id;
    csi.evalScript("exportAIHtml()", function(result){
      alert(result);
    });//main()
  });

getTheme is a method that returns a value and then I'm able to run .then. First part works correctly. csi.evalScript is a method prebuilt into a module I'm using. It returns a callback so I can use the result. For code clarity I would like to use the result in another then.

I've attempted this:

  let getThemeId = getMainThemeId();
  getThemeId
  .then(id => {
    themeId = id;
    return csi.evalScript("exportAIHtml()", function(result){
      return (result);
    })
    .then(result => alert(result));

The alert there is appearing as undefined so obviously it's not waiting for it to finish executing. Would this have to be wrapped in a new promise to get it to work as expected? Shouldn't it just chain it and continue the then?

FabricioG
  • 3,107
  • 6
  • 35
  • 74
  • You can't return from a callback like that. Either wrap it in a `new Promise` yourself, or use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original). – jonrsharpe Oct 31 '19 at 19:06
  • Yes, you'd use `new Promise(resolve => { csi.evalScript("exportAIHtml()", resolve); })` – Bergi Oct 31 '19 at 19:08
  • Btw, don't use a global `themeId` variable that you assign to from inside an asynchronous callback. – Bergi Oct 31 '19 at 19:09
  • Ok thanks or the tip @Bergi – FabricioG Oct 31 '19 at 19:15

0 Answers0