GithubHelp home page GithubHelp logo

async-await-replace-promise's Introduction

async-await-replace-promise

async await 替换 promise的理由

example

const getJson = () => {
    return new Promise((resolve, reject) => {
        let dbJson = {
            name: 'promise'
        }
        try {
            resolve(dbJson)
        } catch {
            reject('err')
        }
    })
}

const MakeRequest = () => {
    getJson().then(data => {
        console.log(data)
         retrun 'done'
    })
}
const MakeRequest = async () => {
    console.log(await getJson())
    return 'done'
}
MakeRequest().then(res => {
    console.log(res)
})

不同之处

  • 函数前面多了一个 async 关键字。await 关键字只能用在 async 定义的函数内。async 函数会隐式地返回一个 promise,该 promise 的 reosolve 值就是函数 return 的值。(示例中 reosolve 值就是字符串”done”)

  • 不能在最外层代码中使用 await,因为不在 async 函数内。

为什么async&await比promise更好

  1. 简洁

使用 Async/Await 明显节约了不少代码。我们不需要写.then,不需要写匿名函数处理 Promise 的 resolve 值,也不需要定义多余的 data 变量,还避免了嵌套代码。这些小的优点会迅速累计起来,这在之后的代码示例中会更加明显。

  1. 错误处理

Async/Await 让 try/catch 可以同时处理同步和异步错误。

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.