今天发现个async的新用法,之前都是搭配着 promise 让一步函数变得同步,今天试了一下下面的写法

async function hello(){
    return "hello world";
}
hello().then(data=>{
    console.log(data + "哈哈");//hello world哈哈
    return 11;
}).then(a=>{
    console.log(a);//11
}).then(()=>{
    console.log("ssd");//ssd
}).then(()=>{
    console.log("啦啦啦");//啦啦啦
})

这样就是 换一种写法而已,并处理不了setTimeOut这种异步,不过可以让函数变成链式的,看着还不错,记录下来,万一有使用场景了。。。

如果想处理异步 还是 async配合promise使用吧

 

相关文章: