Search code examples
javascriptvisual-studio-codeasync-awaites6-promise

Promises to async/await in vscode


I use visual studio code and in those articles:

=> https://dev.to/ben/visual-studio-code-can-now-convert-your-long-chains-of-promisethens-into-asyncawait-automagically-1b1b

=> https://umaar.com/dev-tips/182-typescript-async-await/

=>https://code.visualstudio.com/updates/v1_28

they say there is a feature which is vscode can convert promises to async await itself but I couldn't make it for my .js files. Can you help me?

Have a nice day!


Solution

  • I didn't know it but i just tried in my vscode. When you just click or double click your function, a light bulb appears like in the videos you copied. Then you click on it and click "convert to async..."

    • You need to have javascript.validate.enable set true (vscode -> Preferences -> Settings) (or cmd-, in OSX).

    • I have version 1.30.0 (1.30.0). Check your version.

    • If none of this works maybe there is something wrong with your Promise. Try these examples...

    TS:

    function example(): Promise<boolean> {
      return fetch('https://stackoverflow.com').then( result => result.ok; );
    }
    

    JS:

    function example() 
    { 
      return Promise.resolve(1) 
        .then((value) => { 
          console.log(value) 
        }) 
        .catch(err => { 
          console.log(err); 
        })
    }