0

I am working on text parser. The code is intended to work in a browser. It needs to support an ability to allow user to enter value[s] to be used by the parser while the text is being processed. I found a package dialig-promise to be used for this purpose, but things are not working like I want. My goal is for fillMacro() is to block until the dialog is closed, but it is not happening.

  dataMacro(obj) {
    ...
    fragments.push(myDate);
  },
  fillMacro(obj) {
    console.log(`fillText ${JSON.stringify(obj)}`);
    return new Promise((resolve, reject) => {
      promptPromise("Please enter value for "+ obj.name, obj.defaultValue || '')
      .then(inp => {
        console.log('entered:', inp);
        resolve(inp);
      }, err => {
        console.log("User Escapes. Use default?")
        resolve(obj.defaultValue || '');
      });
    })
    .then(value => {
      fragments.push(value);          
    });
    console.log('finished processing fillText');
  },
  textMacro(obj) {
    ...
    fragments.push(myText);
  }

Please advise.

Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43
  • Promises don't cause anything to block. They just help with writing non-blocking asynchronous code in a sequential manner. – Bergi Oct 29 '17 at 13:15
  • 2
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Oct 29 '17 at 13:16
  • Please show us the code that calls `fillMacro()` - that's where you will need to wait for the promise before moving forward – Bergi Oct 29 '17 at 13:19

1 Answers1

-1

It's not working because you are returning a promise from fillMacro and the code will note wait for your promise to be fulfiled and istead it will continue with the execution. You have multiple solutions for your problem:

  1. Make fillMacro a promise:
    fillMacro = new Promise((resolve, reject) => {
      // make your computations here
    });

and then use it:

    fillMacro.then((success) => {
      // this part will execute when the promise has been resolved 
    });
  1. Use async await

  2. You could also use generators and handle the async part on your own, but I think that this solution is not suitable for your scenario.

Kaleb
  • 17
  • 1
  • 6
Ioan Ungurean
  • 319
  • 1
  • 15
  • 1
    I think #1 can be done even with original code, only instead of variable access it needs to be function call. – Vasan Oct 29 '17 at 12:59
  • You are right, he will have to remove `then` and use it like so: `fillMacro().then(() => {})` – Ioan Ungurean Oct 29 '17 at 13:01
  • 1
    I would absolutely recommend to avoid #3 now that we have `async`/`await`. – Bergi Oct 29 '17 at 13:18
  • Doing ```asyncFunc = async () => { let value = await promptPromise("Please enter value for "+ obj.name, obj.defaultValue || ''); return value; }; asyncFunc() .then(value => { console.log('finished processing fillText, value:', value); fragments.push(value); });``` obviously does not help. Essentially, it is the same thing as the original albeit with slightly different syntax. – Moshe Shmukler Oct 29 '17 at 15:39