Giter VIP home page Giter VIP logo

Comments (5)

stevejroberts avatar stevejroberts commented on May 20, 2024

Not necessarily disagreeing but I'm not clear why 'then' (or any task/promise continuation syntax) is considered 'legacy' or not as clear?

Is there a typescript style guide documented somewhere we should follow and discuss to come up with the right set of tslint settings?

from aws-toolkit-vscode.

mpiroc avatar mpiroc commented on May 20, 2024

async/await is preferred because it allows you to write flat code, rather than adding a level of nesting for each asynchronous call. This makes asynchronous code much easier to read, write, and reason about. The awkward nesting of Tasks/Promises/etc. is the motivation for the async/await feature in the first place.

I don't see await vs then specifically called out in any popular style guide, but any async/await guide will explain the advantages. Some examples:

I don't think that a tslint rule is appropriate, because there are still cases where you might want then:

const promises = []
for (const i = 0; i < 10; i++) {
    promises.append(makePromise(i).then(...))
}

await Promise.all(promises)

You could avoid then by extracting makePromise(i).then(...) into a helper function, but using then here is simpler.

This issue is more about cases like this:

foo(): Promise<string> {
    return somePromise.then(result => {
        return `Hello, ${result}!`
    })
}

Compare to:

async foo(): Promise<string> {
    const result = await somePromise
    return `Hello, ${result}!`
}

from aws-toolkit-vscode.

awschristou avatar awschristou commented on May 20, 2024

I have a preference towards async/await as it seems to cut down on explicit Promise usage. In one case, I had wrapped a function in a Promise constructor, which accidentally ate thrown Errors. I chalk that up to gaining familiarity with the language though too.

VS Code Extension development supports promises broadly - https://code.visualstudio.com/docs/extensionAPI/patterns-and-principles#_promises

from aws-toolkit-vscode.

stevejroberts avatar stevejroberts commented on May 20, 2024

As I said, I don't necessarily disagree. I checked with our Javascript SDK team regarding your use of the term 'legacy' for 'then' usage and they don't consider it such and in fact raised the same discussion that async/await is preferred but sometimes 'then' is useful. Therefore I don't think a broad 'must not use then()' mandate is appropriate.

Our style guide, when we have one, should therefore likely state 'prefer async/await over then()' and show the examples you list as the reason why. Perhaps this issue should mutate into one that creates said style guide in the repo.

from aws-toolkit-vscode.

mpiroc avatar mpiroc commented on May 20, 2024

@steveataws Thanks, that sounds good. I worded this issue poorly--I agree that "prefer" is the right terminology.

from aws-toolkit-vscode.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.